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 pathBOJ15686.java
More file actions
79 lines (68 loc) · 2.46 KB
/
Copy pathBOJ15686.java
File metadata and controls
79 lines (68 loc) · 2.46 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ15686 {
static int n;
static int m;
static int homeX[] = new int[2501];
static int homeY[] = new int[2501];
static int homeIndex = 0;
static int chickenX[] = new int[14];
static int chickenY[] = new int[14];
static int chickenIndex = 0;
static boolean selected[] = new boolean[14];
static int answer = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= n; j++) {
int num = Integer.parseInt(st.nextToken());
if (num == 1) {
homeX[homeIndex] = i;
homeY[homeIndex] = j;
homeIndex++;
}
if (num == 2) {
chickenX[chickenIndex] = i;
chickenY[chickenIndex] = j;
chickenIndex++;
}
}
}
for (int i = 0; i <= chickenIndex - m; i++) {
selected[i] = true;
dfs(i, 1);
selected[i] = false;
}
System.out.println(answer);
}
private static void dfs(int index, int len) {
if (len == m) {
int sum = 0;
for (int i = 0; i < homeIndex; i++) {
int min = Integer.MAX_VALUE;
for (int j = 0; j < chickenIndex; j++) {
if (selected[j]) {
min = Math.min(min, distance(homeX[i], homeY[i], chickenX[j], chickenY[j]));
}
}
sum += min;
}
answer = Math.min(answer, sum);
return;
}
for (int i = index + 1; i <= chickenIndex - m + len + 1; i++) {
selected[i] = true;
dfs(i, len + 1);
selected[i] = false;
}
}
private static int distance(int x1, int y1, int x2, int y2) {
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
}