-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_51_41.cpp
More file actions
54 lines (52 loc) · 1.45 KB
/
LeetCode_51_41.cpp
File metadata and controls
54 lines (52 loc) · 1.45 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// T(n) = O(n!) S(n) = O(n)
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
int row;
vector<int> col(n, 0);
vector<int> diag(2 * n - 1);
vector<int> antiDiag(2 * n - 1);
// 错误点1: 注意是back_inserter
fill_n(back_inserter(curr), n, string(n, '.'));
numQueen = n;
dfs(0, col, diag, antiDiag);
return res;
}
private:
vector<vector<string>> res;
vector<string> curr;
int numQueen;
void dfs(int row,
vector<int>& col,
vector<int>& diag,
vector<int>& antiDiag)
{
// terminator
if (row >= numQueen)
{
res.push_back(curr);
return;
}
// process current
for (int i = 0; i < numQueen; i++)
{
if (col[i] == 0 &&
// 错误点2:row + i即可,不需要加nunQueens - 1,因为不会越界
diag[row + i] == 0 &&
antiDiag[row - i + numQueen - 1] == 0)
{
col[i] = 1;
diag[row + i] = 1;
antiDiag[row - i + numQueen - 1] = 1;
curr[row][i] = 'Q';
// drill down
dfs(row + 1, col, diag, antiDiag);
// restore
col[i] = 0;
diag[row + i] = 0;
antiDiag[row - i + numQueen - 1] = 0;
curr[row][i] = '.';
}
}
}
};