-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#0051.n-queens.cpp
34 lines (29 loc) · 1.21 KB
/
#0051.n-queens.cpp
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
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
this->index = n;
vector<string> cursor(n, string(n, '.'));
vector<vector<string>> result;
vector<int> used(n + 2*n-1 + 2*n-1, false);
updateBoard(used, 0, cursor, result);
return result;
}
int index;
void updateBoard(vector<int>& used, int index,
vector<string>& cursor,
vector<vector<string>>& result) {
if (index == this->index) {
result.push_back(cursor);
} else {
for (int c = 0; c < this->index; ++c) {
if(!used[c] && !used[this->index+index-c+this->index-1] && !used[this->index+2*this->index-1+index+c]) {
cursor[index][c] = 'Q';
used[c] = used[this->index+index-c+this->index-1] = used[this->index+2*this->index-1+index+c] = true;
updateBoard(used, index+1, cursor, result);
used[c] = used[this->index+index-c+this->index-1] = used[this->index+2*this->index-1+index+c] = false;
cursor[index][c] = '.';
}
}
}
};
};