-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathN-Queens.cpp
50 lines (46 loc) · 1.3 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
vector<vector<string>> ans;
bool isQ(vector<string> b, int x, int y) {
// cout<<x<<" "<<y<<"\n";
if (x < 0 || x >= b.size() || y < 0 || y >= b.size()) return false;
if (b[x][y] == 'Q') return true;
return false;
}
bool canPlace(vector<string> b, int x, int y) {
for (int i = 0; i < b.size(); i++) {
if (b[i][y] == 'Q') return false;
}
for (int i = 0; i < b.size(); i++) {
if (isQ(b, x + i, y + i) || isQ(b, x + i, y - i) ||
isQ(b, x - i, y - i) || isQ(b, x - i, y + i))
return false;
}
return true;
}
void nq(vector<string> b, int l, int n) {
// cout<<l<<"\n";
if (l == n) {
ans.push_back(b);
return;
}
for (int i = 0; i < n; i++) {
if (canPlace(b, l, i)) {
b[l][i] = 'Q';
nq(b, l + 1, n);
b[l][i] = '.';
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<string> b;
for (int i = 0; i < n; i++) {
b.push_back("");
for (int j = 0; j < n; j++) {
b[i].push_back('.');
}
}
nq(b, 0, n);
return ans;
}
};