This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPROG12952.java
More file actions
55 lines (50 loc) · 1.54 KB
/
PROG12952.java
File metadata and controls
55 lines (50 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class PROG12952 {
static int[][] map;
static int num;
static boolean[] visited;
static boolean[][] visited2;
static int answer = 0;
public static void main(String[] args) {
int n = 4;
int answer = solution(n);
System.out.println(answer);
}
public static int solution(int n) {
map = new int[n][n];
num = n;
for (int i = 0; i < n; i++) {
visited = new boolean[n];
visited2 = new boolean[n][n];
visited[i] = true;
visited2[0][i] = true;
dfs(0, i, 1);
}
return answer;
}
static void dfs(int x, int y, int length) {
if (length == num) {
answer++;
}
else {
for (int i = 0; i < num; i++) {
if (!visited[i]) {
boolean canDo = true;
for (int j = 0; j <= x; j++) {
for (int k = 0; k < num; k++) {
if (visited2[j][k] && (Math.abs(j - x - 1) == Math.abs(k - i))) {
canDo = false;
}
}
}
if (canDo) {
visited2[x + 1][i] = true;
visited[i] = true;
dfs(x + 1, i, length + 1);
}
}
}
}
visited[y] = false;
visited2[x][y] = false;
}
}