ohmy.codes

IP 121.168.0.0 Created 05-20 12:45
1
import java.io.*;
2
class Main{
3
static boolean[][] chess;
4
static int n,res=0;
5
static void queen(int y,int x){
6
chess[y][x]=true;
7
int i=y+1,j;
8
for(;i<n;i++){
9
chess[i][x]=true; //세로
10
}
11
12
i=y+1; j=x-1;
13
while(!(i>=n||j<0)){ //대각선 왼아래
14
chess[i++][j--]=true;
15
}
16
17
i=y+1; j=x+1;
18
while(!(i>=n||j>=n)){ //대각선 오아래
19
chess[i++][j++]=true;
20
}
21
}
22
static boolean[][] cp(int y,int x){
23
boolean[][] copy=new boolean[n][3];
24
int i=y,j,id=0;
25
for(;i<n;i++){
26
copy[id++][0]=chess[i][x]; //세로
27
}
28
i=y;j=x;id=0;
29
while(!(i>=n||j<0)){ //대각선 왼아래
30
copy[id++][1]=chess[i++][j--];
31
}
32
i=y;j=x;id=0;
33
while(!(i>=n||j>=n)){ //대각선 오아래
34
copy[id++][2]=chess[i++][j++];
35
}
36
return copy;
37
}
38
static boolean[][] ch(int y,int x,boolean[][] copy){
39
int i=y,j,id=0;
40
for(;i<n;i++){
41
chess[i][x]=copy[id++][0]; //세로
42
}
43
i=y;j=x;id=0;
44
while(!(i>=n||j<0)){ //대각선 왼아래
45
chess[i++][j--]=copy[id++][1];
46
}
47
i=y;j=x;id=0;
48
while(!(i>=n||j>=n)){ //대각선 오아래
49
chess[i++][j++]=copy[id++][2];
50
}
51
return copy;
52
}
53
static void nq(int dt){
54
if(dt==n){
55
res++;
56
return;
57
}
58
for(int i=0;i<n;i++){
59
if(!chess[dt][i]){
60
boolean[][] copy=cp(dt,i);
61
queen(dt,i);
62
nq(dt+1);
63
ch(dt,i,copy);
64
}
65
}
66
}
67
68
public static void main(String[] args)throws Exception{
69
BufferedReader I=new BufferedReader(new InputStreamReader(System.in));
70
71
n=Integer.parseInt(I.readLine());
72
73
chess=new boolean[n][n];
74
nq(0);
75
76
System.out.println(res);
77
}
78
}