八皇后经典算法题,个人写的题解以及解析,非常的清楚了,直接上我写的代码
时间复杂度O(1)
/**
* @Author-- Skipper
*/
class Solution {
/**
* 记录某列是否已有皇后摆放
*/
private boolean col[];
/**
* 补充说明: x和y分别是横纵坐标, n是已经摆放好的棋子的数目
*
* 记录某条正对角线(左上右下)是否已有皇后摆放(某条对角线对应的摆放位置为 x - y + n - 1)
* 即对应某条(左上右下)对角线
*/
private boolean dp1[];
/**
* 记录某条斜对角线(左下右上)是否已有皇后摆放(某条对角线对应的摆放位置为 x + y)
*/
private boolean dp2[];
public int totalNQueens(int n) {
// 这里默认col[],dp1[],dp2[]的值都是false
col = new boolean[n];
dp1 = new boolean[2 * n - 1];
dp2 = new boolean[2 * n - 1];
return putQueen(n, 0);//其中,n是我们还没有摆放的皇后数量,0是我们已经摆放完的数量
}
/**
* 递归回溯方式摆放皇后
*
* @param n 待摆放皇后个数
* @param index 已摆放皇后个数
*/
private int putQueen(int n, int index) {
int res = 0;//输出结果 res
if (index == n) {
return 1;//如果待摆放的皇后 = 已经摆放的皇后,表示成功摆放完,返回这1种可能性。
}
// 表示在 index 行的第 i 列尝试摆放皇后
for (int i = 0; i < n; i++) {
if (!col[i] && !dp1[i - index + n - 1] && !dp2[i + index]) {
// 递归
col[i] = true;
dp1[i - index + n - 1] = true;
dp2[i + index] = true;
res += putQueen(n, index + 1);//递归入口
//如果对之前的进行回溯
col[i] = false;
dp1[i - index + n - 1] = false;
dp2[i + index] = false;
}
}
return res;
}
}
测试类
如果还没有明白,这个测试类加上去,两个代码扔到eclipse或idea上debug
/**
* @Author-- Skipper
*/
public class Main {
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.totalNQueens(8));//输出结果
}
}
写这么多感觉有点亢余