public class EightQueen {
public static int count;
public static int max = 8;
public static int[] row = new int[max];
public static void main(String[] args) {
check(0);
System.out.println("一共有" + count + "种解法~");
}
public static void check(int n) {
if (n == 8) {
count++;
print();
return;
}
for (int i = 0; i < max; i++) {
row[n] = i;
if (judge(n)) {
check(n + 1);
}
}
}
public static boolean judge(int n) {
for (int i = 0; i < n; i++) {
if (row[i] == row[n] || Math.abs(n - i) == Math.abs(row[i] - row[n])) {
return false;
}
}
return true;
}
public static void print() {
for (int data : row) {
System.out.print(data + " ");
}
System.out.println();
}
}