八皇后问题代码实现
package com.pionner.recursion;
public class Queen8 {
int max = 8;
int[] array = new int[max];
static int i = 0;
public static void main(String[] args) {
Queen8 queen8 = new Queen8();
queen8.check(0);
System.out.println(i);
}
private void print() {
for (int item : array) {
System.out.print(item + " ");
}
System.out.println();
}
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 check(int n) {
if (n == max) {
i++;
print();
return;
}
for (int i = 0; i < max; i++) {
array[n] = i;
if (judge(n)) {
check(n+1);
}
}
}
}