十四、八皇后问题
1、介绍:在 8
×
8
格的国际象棋上摆放八个皇后,使其不能互相攻击,即:
任意两个皇后都不能处于同一行、
同一列或同一斜线上,问有多少种摆法(92)。
2、思路分析
1) 第一个皇后先放第一行第一列
2) 第二个皇后放在第二行第一列、然后判断是否 OK
, 如果不
OK
,继续放在第二列、第三列、依次把所有列都放完,找到一个合适
3) 继续第三个皇后,还是第一列、第二列……直到第 8
个皇后也能放在一个不冲突的位置,算是 找到了一个正确解
4) 当得到一个正确解时,在栈回退到上一个栈时,就会开始回溯,即将第一个皇后,放到第一列的所有正确解,全部得到.
5) 然后回头继续第一个皇后放第二列,后面继续循环执行 1,2,3,4
的步骤
3、代码实现
public class Queue8 {
int max = 8;
int[] array = new int[max];
static int count = 0;
static int judgeCount = 0;
public static void main(String[] args) {
Queue8 queue8 = new Queue8();
queue8.check(0);
System.out.printf("一共有%d解法", count);
System.out.printf("一共判断冲突的次数%d次", judgeCount); // 1.5w
}
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) {
judgeCount++;
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() {
count++;
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}