-
Notifications
You must be signed in to change notification settings - Fork 0
/
191230-1.cpp
48 lines (46 loc) · 847 Bytes
/
191230-1.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
// https://leetcode-cn.com/problems/n-queens-ii/
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int totalNQueens(int n) {
int count = 0;
if (n > 0) {
vector<int> m;
for (int i = 0; i < n; ++i) {
m.push_back(-1);
}
int i = 0;
for (;;) {
next:
if (++m[i] >= n) {
m[i] = -1;
if (--i < 0) break;
continue;
}
for (int j = 0; j < i; ++j) {
if (m[j] == m[i]) goto next;
if (abs(m[j] - m[i]) == abs(j - i)) goto next;
}
if (i == n - 1) {
++count;
} else {
++i;
}
}
}
return count;
}
};
int main()
{
Solution s;
printf("%d\n", s.totalNQueens(1));
printf("%d\n", s.totalNQueens(2));
printf("%d\n", s.totalNQueens(3));
printf("%d\n", s.totalNQueens(4));
printf("%d\n", s.totalNQueens(5));
return 0;
}