File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments