From 22ec273f5754b4f85401db0d238a108c24d53f67 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:01:50 +0900 Subject: [PATCH] =?UTF-8?q?[20250609]=20BOJ=20/=20G4=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/202506/09 BOJ G4 N-Queen | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 suyeun84/202506/09 BOJ G4 N-Queen diff --git a/suyeun84/202506/09 BOJ G4 N-Queen b/suyeun84/202506/09 BOJ G4 N-Queen new file mode 100644 index 00000000..21600433 --- /dev/null +++ b/suyeun84/202506/09 BOJ G4 N-Queen @@ -0,0 +1,44 @@ +```java +import java.io.*; +import java.util.*; + +public class boj9663 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N; + static int[] position; + static int answer = 0; + + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + position = new int[N]; + backtracking(0); + System.out.println(answer); + } + + public static void backtracking(int cnt) { + if (cnt == N) { + answer++; + return; + } + + for (int i = 0; i < N; i++) { + position[cnt] = i; + boolean flag = true; + for (int j = 0; j < cnt; j++) { + if (position[cnt] == position[j] || Math.abs(position[cnt] - position[j]) == Math.abs(cnt - j)) { + flag = false; + break; + } + } + if (flag) { + backtracking(cnt + 1); + } + } + } +} +```