-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9963.cpp
47 lines (40 loc) · 927 Bytes
/
9963.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
//9963 N-Queen
#include <iostream>
#include <vector>
using namespace std;
int n;
int cnt;
vector<bool> isused1; //열
vector<bool> isused2; // 우향 대각선 / x+y
vector<bool> isused3; // 좌향 대각선 \ x-y+n-1
void nqueen(int cur)
{
if(cur == n)//다 놨다면
{
cnt++;
return;
}
for(int i = 0; i<n; i++)
{
if(isused1[i]||isused2[cur+i]||isused3[cur-i+n-1]) continue;
isused1[i] = 1;
isused2[cur+i] = 1;
isused3[cur-i+n-1]=1; //사용 체크
nqueen(cur+1); //다음열루 들어감
isused1[i] = 0; //나왔다고 체크
isused2[cur+i] = 0;
isused3[cur-i+n-1]=0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
isused1 = vector<bool>(n, false);
isused2 = vector<bool>(2*n-1, false);
isused3 = vector<bool>(2*n-1, false);
nqueen(0);
cout << cnt;
return 0;
}