-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ9663.java
More file actions
44 lines (36 loc) · 717 Bytes
/
Copy pathBOJ9663.java
File metadata and controls
44 lines (36 loc) · 717 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
package day7.backtracking;
import java.util.Scanner;
public class BOJ9663 {
static int N;
static int total=0;
static int[] arr;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
N=sc.nextInt();
arr=new int[N];
queen(0);
System.out.println(total);
}
private static void queen(int cnt) {
if(cnt==N) {
total++;
return;
}
for (int i = 0; i < N; i++) {
arr[cnt]=i;
if(isAvailable(cnt)) {
queen(cnt+1);
}
}
}
private static boolean isAvailable(int col) {
for (int i = 0; i < col; i++) {
if(arr[col]==arr[i]) {
return false;
}else if(Math.abs(col-i)==Math.abs(arr[col]-arr[i])) {
return false;
}
}
return true;
}
}