-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-queens.cpp
39 lines (35 loc) · 1.06 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
class Solution {
public:
int n, wMask;
vector<vector<string> > result;
vector<vector<string> > solveNQueens(int n) {
this->n = n;
this->wMask = (1<<n)-1;
int total = run(0, 0, 0, n);
return result;
}
private:
int run(int left, int down, int right, int level, vector<string> trace = vector<string>()){
if (level == 0) {
result.push_back(trace);
return 1;
}
int pos = (left | down | right) & wMask;
if (pos == wMask) return 0;
int total = 0;
for (int i = 0; i < n; i++){
int setbit = (1 << i);
if ((pos & setbit) != 0)
continue;
trace.push_back(toStringRow(i, n));
total += run((left|setbit) << 1, down|setbit, (right | setbit) >> 1, level - 1, trace);
trace.pop_back();
}
return total;
}
string toStringRow(int p, int w){
string s (w, '.');
s[p] = 'Q';
return s;
}
};