-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN-Queens.cpp
55 lines (55 loc) · 1.28 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
51
52
53
54
55
class Solution {
vector<vector<string> > r;
bool check(vector<string> q, int row, int col, int n) {
for (int i = 0; i < row; i++) {
if (q[i][col] == 'Q')
return false;
int left = col - row + i;
if (left >= 0 && left < n && q[i][left] == 'Q')
return false;
int right = row - i + col;
if (right >= 0 && right < n && q[i][right] == 'Q')
return false;
}
return true;
}
void put(vector<string> q, int row, int n) {
if (row == n) {
vector<string> vq;
for (int i = 0; i < n; i++) {
string s = "";
for (int j = 0; j < n; j++) {
s = s + q[i][j];
}
vq.push_back(s);
}
r.push_back(vq);
return;
}
for (int i = 0; i < n; i++) {
if (q[row][i] == '.') {
q[row][i] = 'Q';
if (check(q, row, i, n)) {
put(q, row + 1, n);
}
q[row][i] = '.';
}
}
}
public:
vector<vector<string> > solveNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
r.clear();
vector<string> q;
for (int i = 0; i < n; i++) {
string s = "";
for (int j = 0; j < n; j++) {
s = s + ".";
}
q.push_back(s);
}
put(q, 0, n);
return r;
}
};