-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn-queens-ii.cpp
More file actions
68 lines (58 loc) · 1.52 KB
/
n-queens-ii.cpp
File metadata and controls
68 lines (58 loc) · 1.52 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
#include <iostream>
#include <vector>
using namespace std;
// 执行用时 :4 ms, 在所有 C++ 提交中击败了92.78%的用户
// 内存消耗 :8.2 MB, 在所有 C++ 提交中击败了72.24%的用户
class Solution {
public:
int totalNQueens(int n) {
vector<int> pos = vector<int>();
int total = 0;
dfs(pos, 0, total, n);
return total;
}
/**
* 搜索加剪枝
*/
void dfs(vector<int> &pos, const int level, int &total, const int &size) {
// 停止条件
if (level >= size) {
total += 1;
return;
}
// 剪枝搜索
for (int i = 0; i < size; i++) {
// 不合法位置跳过
if (notValid(pos, i, level)) {
continue;
}
// 合法位置递归
pos.push_back(i);
dfs(pos, level + 1, total, size);
pos.pop_back();
}
}
/**
* 不合法的位置
* 1. 斜线上: abs(dist x)相等abs(dist y)
* 2. 直线上: x相等或y相等
*/
bool notValid(vector<int> &pos, int idx, int level) {
for (int x = 0; x < pos.size(); x++) {
int y = pos[x];
if (y == idx) {
return true;
}
if (abs(y - idx) == abs(x - level)) {
return true;
}
}
return false;
}
};
int main() {
Solution s = Solution();
cout << s.totalNQueens(8) << endl;
cout << s.totalNQueens(4) << endl;
return 0;
}