-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_9663.java
More file actions
63 lines (52 loc) ยท 1.27 KB
/
_9663.java
File metadata and controls
63 lines (52 loc) ยท 1.27 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package backjoon;
// https://www.acmicpc.net/problem/9663
// N-Queen
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _9663 {
public static int[] arr;
public static int N;
public static int count = 0;
public static void main(String[] args) throws IOException {
// memory 12052 runtime 5728
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N];
nQueen(0);
System.out.println(count);
}
public static void nQueen(int depth) {
// ๋ชจ๋ ์์๋ฅผ ๋ค ์ฑ์ด ์ํ๋ฉด count ์ฆ๊ฐ ๋ฐ return
if (depth == N) {
count++;
return;
}
for (int i = 0; i < N; i++) {
arr[depth] = i;
// ๋์ ์ ์๋ ์์น์ผ ๊ฒฝ์ฐ ์ฌ๊ทํธ์ถ
if (Possibility(depth)) {
nQueen(depth + 1);
}
}
}
public static boolean Possibility(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;
}
}
/*
input
8
output
92
*/