-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode51.cpp
313 lines (282 loc) · 9.12 KB
/
leetcode51.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// most important idea in validate
// whether two queens are on the same slash or not
// the absolute distance between two columns is same to two rows or not
// beats over 55.13% of submission
class Solution {
private:
vector<int> sublist;
vector<vector<string>> result;
decltype(auto) construct(vector<int>& sublist){
vector<string> result;
for(auto i = sublist.begin(); i != sublist.end(); i++){
string str = "";
for(int j = 0; j < sublist.size(); j++){
str += (j == *i ? "Q" : ".");
}
result.push_back(str);
}
return result;
}
decltype(auto) validate(vector<int>& sublist, int col){
int row = sublist.size();
for(int i = 0; i < row; i++){
if(col == sublist.at(i)) return false;
if(abs(row - i) == abs(col - sublist.at(i))) return false;
}
return true;
}
void dfs(int n, vector<int>& sublist, vector<vector<string>>& result){
if(sublist.size() == n){
result.push_back(construct(sublist));
return;
}
for(int i = 0; i < n; i++){
if(!validate(sublist, i)) continue;
sublist.push_back(i);
dfs(n, sublist, result);
sublist.pop_back();
}
}
public:
vector<vector<string>> solveNQueens(int n) {
if(n <= 0){
vector<string> s;
result.push_back(s);
return result;
}
dfs(n, sublist, result);
return result;
}
};
}
// idea: col[position], pie[position], na[position]
// we don't need to validate all the back rows each time place a queen on the new row!
// beats over 32% of submission
class Solution {
private:
vector<int> sublist;
vector<vector<string>> result;
decltype(auto) construct(vector<int>& sublist){
vector<string> result;
for(auto i = sublist.begin(); i != sublist.end(); i++){
string str = "";
for(int j = 0; j < sublist.size(); j++){
str += (j == *i ? "Q" : ".");
}
result.push_back(str);
}
return result;
}
void dfs(int n,
int row,
vector<bool>& col,
vector<bool>& pie,
vector<bool>& na,
vector<int>& sublist,
vector<vector<string>>& result){
if(row >= n){
result.reserve(result.size() + 1);
result.push_back(construct(sublist));
return;
}
for(int pos = 0; pos != n; pos++){
int i = row + pos;
int j = n - 1 + pos - row;
if(col.at(pos) == true || pie.at(i) == true || na.at(j) == true) continue;
col.at(pos) = true;
pie.at(i) = true;
na.at(j) = true;
sublist.push_back(pos);
dfs(n, row+1, col, pie, na, sublist, result);
col.at(pos) = false;
pie.at(i) = false;
na.at(j) = false;
sublist.pop_back();
}
}
public:
vector<vector<string>> solveNQueens(int n) {
if(n <= 0){
vector<string> s;
result.push_back(s);
return result;
}
vector<bool> col(n);
vector<bool> pie(2*n - 1);
vector<bool> na(2*n - 1);
dfs(n, 0, col, pie, na, sublist, result);
return result;
}
};
// idea: change boolean array to bit array
// beats over 100% of submission
class Solution {
private:
void dfs(int n,
int row,
int col,
int pie,
int na,
vector<string>& sublist,
vector<vector<string>>& result){
if(row >= n){
result.push_back(sublist);
return;
}
for(int pos = 0; pos < n; pos++){
int i = row + pos;
int j = n - 1 + pos - row;
// 0 means seat is still available
// if the seat is 1, it means the seat is already be taken
if((((col >> pos) | (pie >> i) | (na >> j)) & 1) != 0) continue;
col ^= (1 << pos); // reverse the bit
pie ^= (1 << i);
na ^= (1 << j);
sublist.at(row).at(pos) = 'Q';
dfs(n, row+1, col, pie, na, sublist, result);
col ^= (1 << pos);
pie ^= (1 << i);
na ^= (1 << j);
sublist.at(row).at(pos) = '.';
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<string> sublist(n, string(n, '.'));
vector<vector<string>> result;
if(n <= 0){
vector<string> s;
result.push_back(s);
return result;
}
int col = 0;
int pie = 0;
int na = 0;
dfs(n, 0, col, pie, na, sublist, result);
return result;
}
};
// idea: 請參考leetcode 52
// beats over 83% of submission
class Solution {
private:
vector<int> sublist;
vector<vector<string>> result;
decltype(auto) construct(vector<int>& sublist){
vector<string> subresult;
for(auto i = sublist.begin(); i != sublist.end(); i++){
string str = "";
for(int j = 0; j < sublist.size(); j++){
if(j == *i){
str += "Q";
}else{
str += ".";
}
}
subresult.push_back(str);
}
return subresult;
}
void dfs(int n,
int row,
int col,
int pie,
int na,
vector<int>& sublist,
vector<vector<string>>& result){
int available = ((1 << n) - 1) & ~(col | pie | na);
while(available != 0){
int p = available & -available;
available ^= p;
int col_indexQ = log2(p);
sublist.push_back(col_indexQ);
if(row == n-1){
result.push_back(construct(sublist));
sublist.pop_back();
return;
}else{
dfs(n, row+1, (col ^ p), (pie ^ p) >> 1, (na ^ p) << 1, sublist, result);
sublist.pop_back();
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
if(n <= 0){
vector<string> s;
result.push_back(s);
return result;
}
int col = 0;
int pie = 0;
int na = 0;
dfs(n, 0, col, pie, na, sublist, result);
return result;
}
};
}
// 以為上面的解法最強了嗎?
// 還沒!善用C++的黑魔法 template
// 我拿到了 beats over 99% of submission
// 下面的版本跟上面就只差在將construct改成template
template <typename T>
decltype(auto) construct(T& sublist){
vector<string> subresult;
for(auto i = sublist.begin(); i != sublist.end(); i++){
string str = "";
for(int j = 0; j < sublist.size(); j++){
if(j == *i){
str += "Q";
}else{
str += ".";
}
}
subresult.push_back(str);
}
return subresult;
}
}
// 從discussion board上又學到了一種更簡潔的寫法
// idea: 只要善用sublist,根本不需要construct
// same beats over 99% of submission
class Solution {
private:
void dfs(int n,
int row,
int col,
int pie,
int na,
vector<string>& sublist,
vector<vector<string>>& result){
int available = (((1 << n) - 1) & ~(col | pie | na));
while(available != 0){
int p = available & -available;
available ^= p;
int col_indexQ = log2(p);
sublist.at(row).at(col_indexQ) = 'Q';
if(row == n - 1){
result.push_back(sublist);
sublist.at(row).at(col_indexQ) = '.';
return;
}else{
dfs(n, row + 1, (col ^ p), (pie ^ p) >> 1, (na ^ p) << 1, sublist, result);
sublist.at(row).at(col_indexQ) = '.';
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<string> sublist(n, string(n, '.'));
vector<vector<string>> result;
if(n <= 0){
vector<string> s;
result.push_back(s);
return result;
}
int col = 0;
int pie = 0;
int na = 0;
dfs(n, 0, col, pie, na, sublist, result);
return result;
}
};