From 6b12969706df0385cf895aedf0e3b7cd9a9bc83d Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 16 Sep 2025 23:46:58 +0900 Subject: [PATCH] =?UTF-8?q?[20250916]=20PGM=20/=20LV2=20/=20N-Queen=20/=20?= =?UTF-8?q?=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suyeun84/202509/16 PGM LV2 N-Queen.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 suyeun84/202509/16 PGM LV2 N-Queen.md diff --git a/suyeun84/202509/16 PGM LV2 N-Queen.md b/suyeun84/202509/16 PGM LV2 N-Queen.md new file mode 100644 index 00000000..43eb59e4 --- /dev/null +++ b/suyeun84/202509/16 PGM LV2 N-Queen.md @@ -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; + } +} +```