-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP9663N_Queen2.java
More file actions
58 lines (49 loc) · 929 Bytes
/
P9663N_Queen2.java
File metadata and controls
58 lines (49 loc) · 929 Bytes
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
package BOJ;
import java.util.*;
public class P9663N_Queen2 {
static boolean[][] map = new boolean[15][15];
static int ans = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
back(0, n);
System.out.println(ans);
}
public static void back(int row, int n){
if(row==n)
ans+=1;
for(int col = 0;col<n;col++){
map[row][col] = true;
if(check(row, col))
back(row+1, n);
map[row][col] = false;
}
}
public static boolean check(int row, int col){
for(int i=0;i<row;i++){
if(map[i][col]==true)
return false;
}
for(int i=0;i<col;i++){
if(map[row][i]==true)
return false;
}
int x = row-1;
int y = col-1;
while(x>=0 && y>=0){
if(map[x][y]==true)
return false;
x--;
y--;
}
x = row-1;
y = col-1;
while(x>=0 && y>=0){
if(map[x][y]==true)
return false;
x--;
y++;
}
return true;
}
}