-
Notifications
You must be signed in to change notification settings - Fork 17
/
51. N-Queens.cpp
94 lines (81 loc) · 3.23 KB
/
51. 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Backtracking
//Runtime: 12 ms, faster than 59.92% of C++ online submissions for N-Queens.
//Memory Usage: 8 MB, less than 32.76% of C++ online submissions for N-Queens.
class Solution {
public:
int n;
void backtrack(vector<bool>& used_col, vector<bool>& used_pos_diag,
vector<bool>& used_neg_diag, int r,
vector<int>& curpos, vector<vector<string>>& ans){
if(r == n){
vector<string> curans;
for(int i = 0; i < n; ++i){
string row(n, '.');
row[curpos[i]] = 'Q';
curans.push_back(row);
}
ans.push_back(curans);
}else{
for(int c = 0; c < n; ++c){
if(!used_col[c] && !used_pos_diag[r-c+n-1] && !used_neg_diag[r+c]){
curpos.push_back(c);
used_col[c] = true;
used_pos_diag[r-c+n-1] = true;
used_neg_diag[r+c] = true;
backtrack(used_col, used_pos_diag, used_neg_diag, r+1, curpos, ans);
used_col[c] = false;
used_pos_diag[r-c+n-1] = false;
used_neg_diag[r+c] = false;
curpos.pop_back();
}
}
}
};
vector<vector<string>> solveNQueens(int n) {
this->n = n;
vector<int> curpos;
vector<vector<string>> ans;
//in each recursion, r is different, so there must be only one Q in a row
//for each row, need to check if that column is used
vector<bool> used_col(n, false);
//also check if that positive diagonals is used
//there are 2*n-1 positive diagonals
vector<bool> used_pos_diag(2*n-1, false);
//also check if that positive diagonals is used
//there are 2*n-1 negative diagonals
vector<bool> used_neg_diag(2*n-1, false);
backtrack(used_col, used_pos_diag, used_neg_diag, 0, curpos, ans);
return ans;
}
};
//+speed up, vector<bool> -> vector<int>, build ans on the fly
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for N-Queens.
//Memory Usage: 7.2 MB, less than 91.99% of C++ online submissions for N-Queens.
class Solution {
public:
int n;
void backtrack(vector<int>& used, int r,
vector<string>& cur, vector<vector<string>>& ans){
if(r == n){
ans.push_back(cur);
}else{
for(int c = 0; c < n; ++c){
if(!used[c] && !used[n+r-c+n-1] && !used[n+2*n-1+r+c]){
cur[r][c] = 'Q';
used[c] = used[n+r-c+n-1] = used[n+2*n-1+r+c] = true;
backtrack(used, r+1, cur, ans);
used[c] = used[n+r-c+n-1] = used[n+2*n-1+r+c] = false;
cur[r][c] = '.';
}
}
}
};
vector<vector<string>> solveNQueens(int n) {
this->n = n;
vector<string> cur(n, string(n, '.'));
vector<vector<string>> ans;
vector<int> used(n + 2*n-1 + 2*n-1, false);
backtrack(used, 0, cur, ans);
return ans;
}
};