Skip to content

Commit f7fa587

Browse files
authored
Merge pull request #908 from AlgorithmWithGod/suyeun84
[20250916] PGM / LV2 / N-Queen / 김수연
2 parents 0e05147 + 6b12969 commit f7fa587

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
```java
2+
class Solution {
3+
private static int[] board;
4+
private static int answer;
5+
6+
public static int solution(int n) {
7+
board = new int[n];
8+
9+
backTracking(0, n);
10+
11+
return answer;
12+
}
13+
14+
private static void backTracking(int depth, int n) {
15+
if (depth == n) {
16+
answer++;
17+
return;
18+
}
19+
for (int i = 0; i < n; i++) {
20+
board[depth] = i;
21+
if (valid(depth)) {
22+
backTracking(depth + 1, n);
23+
}
24+
}
25+
}
26+
27+
private static boolean valid(int i) {
28+
for (int j = 0; j < i; j++) {
29+
if (board[i] == board[j]) return false;
30+
if (Math.abs(i - j) == Math.abs(board[i] - board[j])) return false;
31+
}
32+
return true;
33+
}
34+
}
35+
```

0 commit comments

Comments
 (0)