forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0051.cpp
More file actions
30 lines (28 loc) · 1.03 KB
/
0051.cpp
File metadata and controls
30 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ans;
vector<string> board(n, string(n, '.'));
vector<bool> cols(n, false);
vector<bool> diag1(2 * n - 1, false);
vector<bool> diag2(2 * n - 1, false);
dfs(0, cols, diag1, diag2, board, ans);
return ans;
}
private:
void dfs(int y, vector<bool>& cols, vector<bool>& diag1, vector<bool>& diag2, vector<string>& board, vector<vector<string>>& ans) {
if (y == cols.size()) {
ans.push_back(board);
return;
}
for (int x = 0; x < cols.size(); x++) {
if (!cols[x] && !diag1[x + y] && !diag2[x - y + cols.size() - 1]) {
board[y][x] = 'Q';
cols[x] = diag1[x + y] = diag2[x - y + cols.size() - 1] = true;
dfs(y + 1, cols, diag1, diag2, board, ans);
cols[x] = diag1[x + y] = diag2[x - y + cols.size() - 1] = false;
board[y][x] = '.';
}
}
}
};