public class Queue8 {
int max = 8;
int num=0;
int[] array = new int[max];
public static void main(String[] args) {
Queue8 q=new Queue8();
q.check(0);
}
private void check(int n) {
if(n==max) {
print();
return;
}
for(int i=0;i<max;i++) {
array[n]=i;
if(judge(n)) {
check(n+1);
}
}
}
private boolean judge(int n) {
for (int i = 0; i < n; i++) {
if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
return false;
}
}
return true;
}
private void print() {
num++;
System.out.print("第"+num+"个解");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
|