This commit is contained in:
Matti 2024-05-20 22:27:58 +02:00
parent 9ff0ded7ea
commit 00200af26b
2 changed files with 23 additions and 43 deletions

View File

@ -3,10 +3,10 @@ package part5.aufg2;
public class Demo {
public static void main(String[] args) {
Queenproblem Q = new Queenproblem();
Q.setField(new int[]{1,0,0,0,0,0,0,0});
Q.solve(0);
Q.solve();
System.out.println("========\n");
Q.setField(new int[]{0,7,0,0,0,0,0,8});
Q.solve();
}
}

View File

@ -6,15 +6,20 @@ public class Queenproblem {
// 0 means empty
// 1-8 is the y position of the Queen
int[] field;
int[][] field2D;
public Queenproblem(){
this.field = new int[8];
makeBoardEmpty();
}
public void setField(int[] field) {
this.field = field;
}
public void makeBoardEmpty(){
this.setField(new int[] {0,0,0,0,0,0,0,0});
}
private int[][] make2D(){
int[][] field = new int[8][8];
@ -26,10 +31,7 @@ public class Queenproblem {
try {
int yPosition = this.field[i];
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
}catch (IndexOutOfBoundsException e){
;
//System.out.println("Ignored Index Error while making it 2D");
}
}catch (IndexOutOfBoundsException e){/* System.out.println("Ignored Index Error while making it 2D") */}
}
return field;
@ -44,10 +46,7 @@ public class Queenproblem {
}
public boolean checkVertical(int col){
if (this.field[col-1] != 0){
return false;
}
return true;
return this.field[col - 1] == 0;
}
public boolean checkHorizontal(int row){
@ -77,9 +76,7 @@ public class Queenproblem {
return false;
}
}
}catch (IndexOutOfBoundsException e){
; // Skip Iteration with IndexError
}
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError */}
// cannot be in the same try block.
// Exceptions in the + direction stop execution of - prematurely
@ -89,9 +86,7 @@ public class Queenproblem {
return false;
}
}
}catch (IndexOutOfBoundsException e){
; // Skip Iteration with IndexError
}
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError*/ }
}
return true;
}
@ -106,52 +101,37 @@ public class Queenproblem {
return false;
}
}
}catch (IndexOutOfBoundsException e){
;
}
}catch (IndexOutOfBoundsException e){}
try {
if (this.field[col-1-i] != 0) {
if (this.field[col-1-i] == row+i) {
return false;
}
}
}catch (IndexOutOfBoundsException e){
;
}
}catch (IndexOutOfBoundsException e){}
}
return true;
}
public boolean isPossible(int row, int col){
if(checkHorizontal(row) == false){
if(!checkHorizontal(row)){
return false;
}
if (checkVertical(col) == false) {
if (!checkVertical(col)) {
return false;
}
// Diagonal \
if (checkFallingDiagonal(row, col) == false){
if (!checkFallingDiagonal(row, col)){
return false;
}
if (checkRisingDiagonal(row, col) == false){
return false;
}
return true;
return checkRisingDiagonal(row, col);
}
public void solve(int queensPlaced){
public void solve(){
for (int col = 1; col < 9; col++) {
if (this.field[col-1] == 0) {
for (int row = 1; row < 9; row++) {
if (isPossible(row,col)) {
this.field[col-1] = row;
queensPlaced++;
this.solve(queensPlaced);
queensPlaced--;
this.solve();
this.field[col-1] = 0;
}
}