[D3] N-Queen - 2806
[D3] N-Queen - 2806
문제 링크
성능 요약
메모리: 13,544 KB, 시간: 11 ms
코드길이: 626 Bytes
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
코드
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
#include<iostream>
using namespace std;
int n;
int cnt;
bool col[13];
bool ldru[25];//n*2
bool lurd[25];
void func(int rw) {
if (rw == n) {
cnt++;
return;
}
for (int cl = 0; cl < n; cl++)
{
if (col[cl]) continue;
if (ldru[rw + cl]) continue;
if (lurd[rw - cl + n - 1]) continue;
col[cl] = true;
ldru[rw + cl] = true;
lurd[rw - cl + n - 1] = true;
func(rw + 1);
col[cl] = false;
ldru[rw + cl] = false;
lurd[rw - cl + n - 1] = false;
}
}
int main() {
int tc;
cin >> tc;
for (int i = 1; i <= tc; i++){
cin >> n;
cnt = 0;
func(0);
cout << "#" << i << " " << cnt << "\n";
}
return 0;
}
This post is licensed under CC BY 4.0 by the author.