# N 皇后

n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。

每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q''.' 分别代表了皇后和空位。

示例 1:

输入:n = 4
输出:
[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释:
如上图所示,4 皇后问题存在两个不同的解法。

示例 2:

输入:n = 1
输出:
[["Q"]]

提示:

以下错误的选项是?

## aop ### before ```c #include using namespace std; ``` ### after ```c int main() { Solution sol; int n = 4; vector> res; res = sol.solveNQueens(n); for (auto i : res) { for (auto j : i) cout << j << " "; cout << endl; } return 0; } ``` ## 答案 ```c class Solution { public: bool isvalid(vector &temp, int i, int j) { for (int k = 0; k < i; ++k) { if (temp[k][j] == 'Q') return false; } for (int p = i - 1, q = j - 1; p >= 0 && q >= 0; --p, --q) { if (temp[p][q] == 'Q') return false; } for (int p = i - 1, q = j + 1; p >= 0 && q < temp.size(); --p, ++q) { if (temp[p][q] == 'Q') return false; } return true; } void dfs(vector> &re, vector &temp, int i, int n) { if (i == n) { re.push_back(temp); return; } for (int j = 0; j < n; ++j) { if (isvalid(temp, i, j)) { temp[i][j] = 'Q'; dfs(re, temp, i, n); } temp[i][j] = '.'; } return; } vector> solveNQueens(int n) { vector> re; string aa; for (int i = 0; i < n; ++i) aa += '.'; vector temp(n, aa); dfs(re, temp, 0, n); return re; } }; ``` ## 选项 ### A ```c class Solution { public: vector> solveNQueens(int n) { vector> res; vector stack(n); vector solution(n, string(n, '.')); dfs(n, 0, stack, solution, res); return res; } private: void dfs(int n, int row, vector &stack, vector &solution, vector> &res) { if (row == n) { res.push_back(solution); } else { for (int i = 0; i < n; i++) { if (row == 0 || !conflict(stack, row, i)) { solution[row][i] = 'Q'; stack[row] = i; dfs(n, row + 1, stack, solution, res); solution[row][i] = '.'; } } } } bool conflict(vector &stack, int row, int col) { for (int i = 0; i < row; i++) { if (col == stack[i] || abs(row - i) == abs(col - stack[i])) { return true; } } return false; } }; ``` ### B ```c class Solution { public: vector> res; vector> solveNQueens(int n) { vector board(n, string(n, '.')); backtrack(board, 0); return res; } void backtrack(vector &board, int row) { if (row == board.size()) { res.push_back(board); return; } int n = board[row].size(); for (int col = 0; col < n; col++) { if (!isValid(board, row, col)) { continue; } board[row][col] = 'Q'; backtrack(board, row + 1); board[row][col] = '.'; } } bool isValid(vector &board, int row, int col) { int n = board.size(); for (int i = 0; i < row; i++) { if (board[i][col] == 'Q') { return false; } } for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { if (board[i][j] == 'Q') { return false; } } for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == 'Q') { return false; } } return true; } }; ``` ### C ```c class Solution { public: bool isValue(vector &pos, int row, int col) { for (int i = 0; i < row; ++i) { if (col == pos[i] || abs(row - i) == abs(col - pos[i])) return false; } return true; } void solveNQueensDFS(vector &pos, int row, vector> &ans) { int n = pos.size(); if (row == n) { vector tmp(n, string(n, '.')); for (int i = 0; i < n; ++i) tmp[i][pos[i]] = 'Q'; ans.push_back(tmp); } else { for (int col = 0; col < n; ++col) { if (isValue(pos, row, col)) { pos[row] = col; solveNQueensDFS(pos, row + 1, ans); pos[row] = -1; } } } } vector> solveNQueens(int n) { vector pos(n, -1); vector> ans; solveNQueensDFS(pos, 0, ans); return ans; } }; ```