-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_9663.java
More file actions
43 lines (39 loc) · 1.03 KB
/
boj_9663.java
File metadata and controls
43 lines (39 loc) · 1.03 KB
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
package backtracking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class boj_9663 {
static int n;
static int[] arr;
static int count=0;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
arr=new int[n];
queen(0);
System.out.println(count);
}
public static void queen(int depth){
if(depth==n){
count++;
return;
}
for(int i=0;i<n;i++){
arr[depth]=i;
if(possible(depth)){
queen(depth+1);
}
}
}
public static boolean possible(int depth){
for(int i=0;i<depth;i++){
if(arr[depth]==arr[i]){
return false;
}
else if(Math.abs(depth-i)==Math.abs(arr[depth]-arr[i])){
return false;
}
}
return true;
}
}