-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Quuen_9663번.CPP
81 lines (58 loc) · 1.04 KB
/
N-Quuen_9663번.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
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
//n queen 9663번
//1행에 1개의 말을 놓는다.
#include<iostream>
using namespace std;
#define MAX_SIZE 15
int map[MAX_SIZE][MAX_SIZE];
int visit[MAX_SIZE][MAX_SIZE];
int n, ret;
bool check(int x, int y) {
//세로 검사
for (int i = 0; i < x; i++) {
if (visit[i][y] == 1) return false;
}
//대각선 검사
int tmp_x, tmp_y;
tmp_x = x;
tmp_y = y;
//우상
while (0 <= tmp_x && tmp_y < n) {
if (visit[tmp_x][tmp_y] == 1) return false;
tmp_x--;
tmp_y++;
}
//좌상
tmp_x = x;
tmp_y = y;
while (0 <= tmp_x && 0 <= tmp_y) {
if (visit[tmp_x][tmp_y] == 1) return false;
tmp_x--;
tmp_y--;
}
return true;
}
void dfs(int x, int y, int d) {
if (visit[x][y] == 1) return;
if (d == n - 1) {
ret++;
return;
}
visit[x][y] = 1;
for (int i = 0; i < n; i++) {
if (check(x + 1, i)) {
dfs(x + 1, i, d + 1);
visit[x + 1][i] = 0;
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin >> n;
ret = 0;
for (int i = 0; i < n; i++) {
dfs(0, i, 0);
visit[0][i] = 0;
}
cout << ret;
return 0;
}