Recursive Queenproblem
This commit is contained in:
parent
b0b248ca9d
commit
3aa56bd3fb
10
src/part5/aufg2/Demo.java
Normal file
10
src/part5/aufg2/Demo.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
61
src/part5/aufg2/Queenproblem.java
Normal file
61
src/part5/aufg2/Queenproblem.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package part5;
|
package part5.aufg2;
|
||||||
|
|
||||||
// I know this is not recursive
|
// I know this is not recursive
|
||||||
// Shut up
|
// Shut up
|
Loading…
Reference in New Issue
Block a user