-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens.cpp
43 lines (38 loc) · 1.11 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
class Solution {
public:
bool check(const vector<int>& queen, int col, int row) {
// check queen[0:row-1]
for (int i = 0; i < row; i++) {
if (queen[i] == col) {
return false;
}
if (abs(queen[i] - col) == row - i) { // diagonal
return false;
}
}
return true;
}
void solveNQueens_(int n, int row, vector<int>* queen,
vector<vector<string>>* sol) {
if (row == n) {
vector<string> new_sol(n, string(n, '.'));
for (int i = 0; i < n; i++) {
new_sol[i][(*queen)[i]] = 'Q';
}
sol->push_back(new_sol);
return;
}
for (int i = 0; i < n; i++) {
if (check(*queen, i, row)) {
(*queen)[row] = i;
solveNQueens_(n, row + 1, queen, sol);
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> sol;
vector<int> queen(n);
solveNQueens_(n, 0, &queen, &sol);
return sol;
}
};