-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens
38 lines (35 loc) · 1.07 KB
/
N-Queens
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
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string>> allSol;
vector<string> sol;
vector<int> col;
solveNQ(n, 0, col, sol, allSol);
return allSol;
}
void solveNQ(int n, int irow, vector<int> &col, vector<string> &sol, vector<vector<string>> &allSol) {
if(irow==n) {
allSol.push_back(sol);
return;
}
for(int icol=0; icol<n; icol++) {
if(validPos(col, irow, icol)) {
string s(n,'.');
s[icol] = 'Q';
sol.push_back(s);
col.push_back(icol);
solveNQ(n, irow+1, col, sol, allSol);
sol.pop_back();
col.pop_back();
}
}
}
bool validPos(vector<int> &col, int irow, int icol) {
if(irow<col.size()) return false;
for(int i=0; i<col.size(); i++) {
if(icol==col[i] || abs(irow-i)==abs(icol-col[i]))
return false;
}
return true;
}
};