-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_Queen.cpp
48 lines (48 loc) · 836 Bytes
/
N_Queen.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
#include <iostream>
using namespace std;
bool a[15][15];
int n;
bool check_col[15];
bool check_dig[40];
bool check_dig2[40];
bool check(int row, int col) {
// |
if (check_col[col]) {
return false;
}
// ¿ÞÂÊ À§ ´ë°¢¼±
if (check_dig[row + col]) {
return false;
}
// /
if (check_dig2[row - col + n]) {
return false;
}
return true;
}
int calc(int row) {
if (row == n) {
// ans += 1;
return 1;
}
int cnt = 0;
for (int col = 0; col<n; col++) {
if (check(row, col)) {
check_dig[row + col] = true;
check_dig2[row - col + n] = true;
check_col[col] = true;
a[row][col] = true;
cnt += calc(row + 1);
check_dig[row + col] = false;
check_dig2[row - col + n] = false;
check_col[col] = false;
a[row][col] = false;
}
}
return cnt;
}
int main() {
cin >> n;
cout << calc(0) << '\n';
return 0;
}