forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens.cc
36 lines (36 loc) · 1.1 KB
/
N-Queens.cc
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
class Solution {
public:
void dfs(vector<vector<string> > &ans, vector<string> &tmp, int row, int leftCross, int rightCross) {
if (row == upperlimit) {
ans.push_back(tmp);
return;
}
int cur = upperlimit & (~(row | leftCross | rightCross));
int nxt, pos;
string str;
while (cur) {
nxt = (cur & (-cur));
cur -= nxt;
pos = nxt + upperlimit + 1;;
str = "";
while (pos > 1) {
str += ((pos & 1) ? 'Q': '.');
pos >>= 1;
}
tmp.push_back(str);
dfs(ans, tmp, row + nxt, (leftCross + nxt) << 1, (rightCross + nxt) >> 1);
tmp.pop_back();
}
}
vector<vector<string> > solveNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string> > ans;
vector<string> tmp;
upperlimit = (1 << n) - 1;
dfs(ans, tmp, 0, 0, 0);
return ans;
}
private:
int upperlimit;
};