Skip to content

Commit 7259e5f

Browse files
authored
Merge pull request #339 from AlgorithmWithGod/suyeun84
[20250609] BOJ / G4 / N-Queen / 김수연
2 parents 0037d8a + 22ec273 commit 7259e5f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

suyeun84/202506/09 BOJ G4 N-Queen

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj9663 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
static int N;
12+
static int[] position;
13+
static int answer = 0;
14+
15+
public static void main(String[] args) throws Exception {
16+
nextLine();
17+
N = nextInt();
18+
position = new int[N];
19+
backtracking(0);
20+
System.out.println(answer);
21+
}
22+
23+
public static void backtracking(int cnt) {
24+
if (cnt == N) {
25+
answer++;
26+
return;
27+
}
28+
29+
for (int i = 0; i < N; i++) {
30+
position[cnt] = i;
31+
boolean flag = true;
32+
for (int j = 0; j < cnt; j++) {
33+
if (position[cnt] == position[j] || Math.abs(position[cnt] - position[j]) == Math.abs(cnt - j)) {
34+
flag = false;
35+
break;
36+
}
37+
}
38+
if (flag) {
39+
backtracking(cnt + 1);
40+
}
41+
}
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)