[BOJ] 9663 N-Queen

Time Lapse :9min 33sec

9663.c

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
#include <stdio.h>

#define gc() getchar_unlocked()
#define pc(x) putchar_unlocked(x)

int answer = 0, N;
int st[15], MAP[15][15];
int fRI(){
int N = gc(), r = 0;
for(;0x30>N||N>0x3A;N=gc());
do{
r = (r<<3)+(r<<1)+(N&0b1111); N = gc();
}while(0x30<=N&&N<=0x3A);
return r;
}

void fWI() {
int r = 0, c = 0;
if(!answer) {pc(48); return ;}
while (!(answer % 10)) { c++; answer /= 10; }
while (answer) { r = (r<<3)+(r<<1)+answer%10; answer /= 10; }
while (r) { pc(r % 10 + 48); r /= 10; }
while (c--) pc(0x30);
return;
}
void DFS(int cur){
if(cur==N) {answer++; return;}
for(int i = 0; i < N; i++){
if(st[i]||MAP[cur][i]) continue;
st[i] ^= 1;
for(int j = 1; j < N-cur; j++){
if(i+j<N) MAP[cur+j][i+j]++;
if(i-j>=0) MAP[cur+j][i-j]++;
}
DFS(cur+1);
st[i] ^= 1;
for(int j = 1; j < N-cur; j++){
if(i+j<N) MAP[cur+j][i+j]--;
if(i-j>=0) MAP[cur+j][i-j]--;
}
}
}
int main(int argc, char** argv){
N = fRI();
DFS(0);
fWI();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/9663/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.