按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 **n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例 1:
输入: n = 4
输出: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释: 如上图所示,4 皇后问题存在两个不同的解法。
示例 2:
输入: n = 1
输出: [["Q"]]
提示:
1 <= n <= 9
题解:
class Solution {
private final List<List<String>> ans = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
int[] col;
boolean[] onPath, diag1, diag2;
col = new int[n];
onPath = new boolean[n];
diag1 = new boolean[n * 2 - 1];
diag2 = new boolean[n * 2 - 1];
dfs(0,n,col,onPath,diag1,diag2);
return ans;
}
private void dfs(int r,int n, int[] col, boolean[] onPath,boolean[] diag1,boolean[] diag2) {
if (r == n) {
List<String> board = new ArrayList<>(n);
for (int c : col) {
char[] row = new char[n];
Arrays.fill(row, '.');
row[c] = 'Q';
board.add(new String(row));
}
ans.add(board);
return;
}
for (int c = 0; c < n; ++c) {
int rc = r - c + n - 1;
if (!onPath[c] && !diag1[r + c] && !diag2[rc]) {
col[r] = c;
onPath[c] = diag1[r + c] = diag2[rc] = true;
dfs(r + 1,n,col,onPath,diag1,diag2);
onPath[c] = diag1[r + c] = diag2[rc] = false; // 恢复现场
}
}
}
}