Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions suyeun84/202509/16 PGM LV2 N-Queen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```java
class Solution {
private static int[] board;
private static int answer;

public static int solution(int n) {
board = new int[n];

backTracking(0, n);

return answer;
}

private static void backTracking(int depth, int n) {
if (depth == n) {
answer++;
return;
}
for (int i = 0; i < n; i++) {
board[depth] = i;
if (valid(depth)) {
backTracking(depth + 1, n);
}
}
}

private static boolean valid(int i) {
for (int j = 0; j < i; j++) {
if (board[i] == board[j]) return false;
if (Math.abs(i - j) == Math.abs(board[i] - board[j])) return false;
}
return true;
}
}
```
Loading