-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
59 lines (51 loc) · 1.51 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
typedef pair<int, int> pii;
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<pii> have;
vector<vector<string>> ans;
string s = "";
for (int i = 0; i < n; i++) s += '.';
auto ok = [&](int x, int y) {
for (auto it : have) {
if (it.second == y) return false;
if (y - x == it.second - it.first) return false;
if (y + x == it.second + it.first) return false;
}
return true;
};
function<void(int)> dfs = [&](int dep) {
if (dep == n) {
vector<string> tmp;
for (int i = 0; i < n; i++) {
tmp.push_back(s);
}
for (auto it : have) {
int x = it.first, y = it.second;
tmp[x][y] = 'Q';
}
ans.push_back(tmp);
return;
}
for (int i = 0; i < n; i++) {
if (ok(dep, i)) {
have.push_back({dep, i});
dfs(dep + 1);
have.pop_back();
}
}
};
dfs(0);
return ans;
}
};