Add VL 5
This commit is contained in:
parent
31b2e986bc
commit
f7f39308e5
@ -5,9 +5,14 @@ die Aufgaben sind in ihm gespeichert und wie in Moodle nummeriert.
|
||||
|
||||
Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medien/Bilder & Co</ins> <br>gespeichert.
|
||||
|
||||
## Bisher vorhanden:
|
||||
## Bisher vollständig vorhanden:
|
||||
- Vorlesung 3 komplett
|
||||
- Vorlesung 4 komplett
|
||||
- Vorlesung 5 1 & 3 von 3
|
||||
|
||||
## Angefangene unfertige Lösungen:
|
||||
- keine
|
||||
- Damenproblem: VL 5 Aufgabe 2
|
||||
<br>Nicht die gewünschte rekursive Backtracking-Implementierung
|
||||
|
||||
|
||||
- Keine
|
||||
|
127
src/part5/aufg2.java
Normal file
127
src/part5/aufg2.java
Normal file
@ -0,0 +1,127 @@
|
||||
package part5;
|
||||
|
||||
// I know this is not recursive
|
||||
// Shut up
|
||||
// It works
|
||||
|
||||
public class aufg2 {
|
||||
public static void QueensPlace(int field[][],int row, int col){
|
||||
|
||||
// block spaces
|
||||
for (int i=0; i<8; i++){
|
||||
// block row and col
|
||||
field[i][col]=3;
|
||||
field[row][i]=3;
|
||||
|
||||
// block diagonals
|
||||
// Left 2 Right, Top 2 Bottom
|
||||
if (row - col + i < 8 && row - col + i >= 0) {
|
||||
field[row-col + i][i]=3;
|
||||
}
|
||||
|
||||
// Left 2 Right, Bottom 2 Top
|
||||
if(row+i < 8 && row+i >=0 && col-i < 8 && col-i >=0){
|
||||
field[row+i][col-i]=3;
|
||||
}
|
||||
if(row-i < 8 && row-i >=0 && col+i < 8 && col+i >=0){
|
||||
field[row-i][col+i]=3;
|
||||
}
|
||||
}
|
||||
// place queen
|
||||
field[row][col]=1;
|
||||
}
|
||||
public static boolean QueensFree(int field[][], int row, int col){
|
||||
return field[row][col] == 0;
|
||||
}
|
||||
public static void QueensPrint(int field[][]){
|
||||
// print field
|
||||
System.out.println("---------------");
|
||||
for(int i=0; i<field.length; i++){
|
||||
for(int j=0; j<field[i].length; j++) {
|
||||
System.out.print(field[i][j]+" ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println("---------------");
|
||||
System.out.println();
|
||||
}
|
||||
public static int[][] QueensSetup(int rows, int cols){
|
||||
|
||||
int field[][] = new int[rows][cols];
|
||||
for(int i = 0; i < rows-1; i++){
|
||||
for(int j = 0; j < cols-1; j++){
|
||||
field[i][j]=0;
|
||||
}
|
||||
}
|
||||
return field;
|
||||
}
|
||||
public static void QueensMain(int queensIn){
|
||||
int rows = 8;
|
||||
int cols = 8;
|
||||
int start_queens = queensIn;
|
||||
int queens = start_queens;
|
||||
int field[][] = QueensSetup(rows,cols);
|
||||
int wrong[][] = new int[8][8];
|
||||
|
||||
if(queens>rows){
|
||||
System.out.println();
|
||||
System.out.println("Zu viele Damen für dieses Feld");
|
||||
System.out.println("maximal:" + rows);
|
||||
return;
|
||||
}
|
||||
if(queens<0) {
|
||||
System.out.println();
|
||||
System.out.println("Ungültiger Input: " + queensIn);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
while(queens>0){
|
||||
boolean full = true;
|
||||
for(int i=0;i<rows;i++){
|
||||
for(int j=0;j<cols;j++){
|
||||
if(queens==0){break;}
|
||||
if(QueensFree(field,i,j)){
|
||||
QueensPlace(field,i,j);
|
||||
queens--;
|
||||
full = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Lösungsversuch: ");
|
||||
QueensPrint(field);
|
||||
if(full){
|
||||
|
||||
// current Queens as wrong for next time
|
||||
for (int i=0; i<8; i++){
|
||||
for(int j=0; j<8; j++){
|
||||
if(field[i][j]==1){
|
||||
wrong[i][j]=3;
|
||||
}
|
||||
}
|
||||
}
|
||||
// reset
|
||||
field = QueensSetup(rows,cols);
|
||||
queens = start_queens;
|
||||
|
||||
// block earlier wrongs
|
||||
for(int i = 0; i<8; i++){
|
||||
for(int j = 0; j<8; j++){
|
||||
if(wrong[i][j]==3){
|
||||
field[i][j]=3;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("New Starting field: ");
|
||||
QueensPrint(field);
|
||||
// aktuelle Positionen d Queens merken und sperren, rest reset
|
||||
}
|
||||
}
|
||||
System.out.println("Finale Lösung: ");
|
||||
QueensPrint(field);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
QueensMain(8);
|
||||
}
|
||||
}
|
35
src/part5/aufg3.java
Normal file
35
src/part5/aufg3.java
Normal file
@ -0,0 +1,35 @@
|
||||
package part5;
|
||||
|
||||
public class aufg3 {
|
||||
|
||||
public static void recursive(int depth){
|
||||
System.out.println(depth);
|
||||
recursive(depth+1);
|
||||
}
|
||||
|
||||
public static void recursive2(int depth){
|
||||
int[] array = new int[100];
|
||||
System.out.println(depth);
|
||||
recursive(depth+1);
|
||||
}
|
||||
|
||||
public static void recursive3(int depth){
|
||||
int var0=3;int var1=3;int var2=3;int var3=3;int var4=3;int var5=3;int var6=3;int var7=3;int var8=3;int var9=3;int var10=3;
|
||||
int var11=3;int var12=3;int var13=3;int var14=3;int var15=3;int var16=3;int var17=3;int var18=3;int var19=3;int var20=3;
|
||||
int var21=3;int var22=3;int var23=3;int var24=3;int var25=3;int var26=3;int var27=3;int var28=3;int var29=3;int var30=3;
|
||||
int var31=3;int var32=3;int var33=3;int var34=3;int var35=3;int var36=3;int var37=3;int var38=3;int var39=3;int var40=3;
|
||||
int var41=3;int var42=3;int var43=3;int var44=3;int var45=3;int var46=3;int var47=3;int var48=3;int var49=3;int var50=3;
|
||||
int var51=3;int var52=3;int var53=3;int var54=3;int var55=3;int var56=3;int var57=3;int var58=3;int var59=3;int var60=3;
|
||||
int var61=3;int var62=3;int var63=3;int var64=3;int var65=3;int var66=3;int var67=3;int var68=3;int var69=3;int var70=3;
|
||||
int var71=3;int var72=3;int var73=3;int var74=3;int var75=3;int var76=3;int var77=3;int var78=3;int var79=3;int var80=3;
|
||||
int var81=3;int var82=3;int var83=3;int var84=3;int var85=3;int var86=3;int var87=3;int var88=3;int var89=3;int var90=3;
|
||||
int var91=3;int var92=3;int var93=3;int var94=3;int var95=3;int var96=3;int var97=3;int var98=3;int var99=3;
|
||||
System.out.println(depth);
|
||||
recursive3(depth+1);
|
||||
}
|
||||
public static void main(String[] args){
|
||||
//recursive(0); // gets to a depth of 20201
|
||||
//recursive2(0); // gets to 20020
|
||||
//recursive3(0); // gets to 9750
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user