Recursive Queenproblem

This commit is contained in:
Matti 2024-05-20 15:52:14 +02:00
parent b0b248ca9d
commit 3aa56bd3fb
3 changed files with 72 additions and 1 deletions

10
src/part5/aufg2/Demo.java Normal file
View File

@ -0,0 +1,10 @@
package part5.aufg2;
public class Demo {
public static void main(String[] args) {
Queenproblem Q = new Queenproblem();
System.out.println(Q.field[2]);
Q.setField(new int[] {0,1,2,3,4,5,6,7});
Q.print();
}
}

View File

@ -0,0 +1,61 @@
package part5.aufg2;
import java.util.Arrays;
public class Queenproblem {
int[] field;
public Queenproblem(){
this.field = new int[8];
}
public void setField(int[] field) {
this.field = field;
}
private int[][] make2D(){
int[][] field = new int[8][8];
for (int[] row : field ){
Arrays.fill(row, 0);
}
for (int i=0; i<8; i++) {
int index = this.field[i];
field[i][index] = 1;
}
return field;
}
public void print(){
int[][] field = make2D();
for (int[] row:field) {
System.out.println(Arrays.toString(row));
}
}
public boolean isPossible(int x, int y){
// Horizontal
for (int value: this.field) {
if (value == y){
return false;
}
}
// Vertical
if (this.field[x] != 0)
return false;
// Diagonal \
for (int i=0; i<8; i++){
try{
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}
}
return true;
}
}

View File

@ -1,4 +1,4 @@
package part5;
package part5.aufg2;
// I know this is not recursive
// Shut up