Skip to content

Commit 3b98755

Browse files
committed
issue #42 9663
1 parent 60eb82c commit 3b98755

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

โ€Žsrc/backjoon/_9663.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/9663
3+
// N-Queen
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
8+
public class _9663 {
9+
10+
public static int[] arr;
11+
public static int N;
12+
public static int count = 0;
13+
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
N = Integer.parseInt(br.readLine());
17+
arr = new int[N];
18+
19+
nQueen(0);
20+
System.out.println(count);
21+
}
22+
23+
public static void nQueen(int depth) {
24+
// ๋ชจ๋“  ์›์†Œ๋ฅผ ๋‹ค ์ฑ„์šด ์ƒํƒœ๋ฉด count ์ฆ๊ฐ€ ๋ฐ return
25+
if (depth == N) {
26+
count++;
27+
return;
28+
}
29+
30+
for (int i = 0; i < N; i++) {
31+
arr[depth] = i;
32+
// ๋†“์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์ผ ๊ฒฝ์šฐ ์žฌ๊ท€ํ˜ธ์ถœ
33+
if (Possibility(depth)) {
34+
nQueen(depth + 1);
35+
}
36+
}
37+
38+
}
39+
40+
public static boolean Possibility(int col) {
41+
42+
for (int i = 0; i < col; i++) {
43+
// ๊ฐ™์€ ํ–‰์— ์กด์žฌํ•  ๊ฒฝ์šฐ
44+
if (arr[col] == arr[i]) {
45+
return false;
46+
}
47+
// ๋Œ€๊ฐ์„ ์ƒ์— ๋†“์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ
48+
else if (Math.abs(col - i) == Math.abs(arr[col] - arr[i])) {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
}
56+
/*
57+
input
58+
8
59+
60+
output
61+
92
62+
*/

0 commit comments

Comments
 (0)