-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn-queens.cpp
More file actions
94 lines (81 loc) · 2.38 KB
/
n-queens.cpp
File metadata and controls
94 lines (81 loc) · 2.38 KB
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
#include <iostream>
#include <vector>
using namespace std;
// 执行用时 :8 ms, 在所有 C++ 提交中击败了95.16%的用户
// 内存消耗 :10.6 MB, 在所有 C++ 提交中击败了47.07%的用户
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result = vector<vector<string>>();
vector<int> pos_y = vector<int>();
dfs(result, pos_y, 0, n);
return result;
}
/**
* search result
*/
void dfs(vector<vector<string>> &result, vector<int> &pos_y, int level, const int &size) {
// find the end
if (level == size) {
// for check
// for (int i = 0; i < pos_y.size(); i++) {
// cout << pos_y[i] << ",";
// }
// cout << endl;
// gen result
vector<string> mat = vector<string>();
for (int x = 0; x < size; x++) {
string row = "";
for (int y = 0; y < size; y++) {
if (y != pos_y[x]) {
row.append(".");
} else {
row.append("Q");
}
}
mat.push_back(row);
}
result.push_back(mat);
return;
}
// dfs search / recursive search
for (int i = 0; i < size; i++) {
if (isNotValid(pos_y, level, i)) {
continue;
}
pos_y.push_back(i);
dfs(result, pos_y, level + 1, size);
pos_y.pop_back();
}
}
/**
* judge wether new idx is valid
* by using calculation
* 1. x == ~x || y == ~y
* 2. abs(x - ~x) == abs(y - ~x)
*/
bool isNotValid(vector<int> &pos_y, int level, int idx) {
int y = 0;
for (int x = 0; x < pos_y.size(); x++) {
y = pos_y[x];
if (y == idx) {
return true;
}
if (abs(x - level) == abs(y - idx)) {
return true;
}
}
return false;
}
};
int main() {
Solution s = Solution();
vector<vector<string>> result = s.solveNQueens(4);
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result[i].size(); j++) {
cout << result[i][j] << endl;
}
cout << "---" << endl;
}
return 0;
}