Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b98755

Browse files
issue sowon-dev#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

Lines changed: 62 additions & 0 deletions
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)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /