方案一、递推
递推 + 回溯
public class NQueen {
private static final int N = 4;
public static void main(String[] args) {
int i, count = 1;
int[] pos = new int[N + 1];
i = 1;
while (i >= 1) {
pos[i]++;
while (pos[i] <= N && !isPlace(pos, i)) {
pos[i]++;
}
if (pos[i] <= N && i == N) {//打印
System.out.println("方案:" + count++);
print(pos);
}
if (pos[i] <= N && i < N) {//考虑下一个皇后
i++;
} else {
pos[i] = 0;
i--;
}
}
}
private static void print(int[] pos) {
for (int i = 1; i < N; i++) {
System.out.print(pos[i] + ",");