-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_15686.java
More file actions
62 lines (52 loc) · 1.86 KB
/
BOJ_15686.java
File metadata and controls
62 lines (52 loc) · 1.86 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOJ_15686 {
static int N, M;
static int[][] dist;
static int answer = Integer.MAX_VALUE;
static List<int[]> homes, chickens;
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());
dist = new int[N][N];
homes = new ArrayList<>();
chickens = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
dist[i][j] = Integer.parseInt(st.nextToken());
if (dist[i][j] == 1) homes.add(new int[]{i, j});
if (dist[i][j] == 2) chickens.add(new int[]{i, j});
}
}
combination(0, chickens.size(), 0, new ArrayList<>());
System.out.println(answer);
}
static void combination(int start, int size, int depth, List<Integer> tmp) {
if (depth == M) {
answer = Math.min(answer, sumDist(tmp));
return;
}
for (int i = start; i < size; i++) {
tmp.add(i);
combination(i + 1, size, depth + 1, tmp);
tmp.remove(tmp.size() - 1);
}
}
static int sumDist(List<Integer> com) {
int tmp = 0;
for (int[] home : homes) {
int dist = Integer.MAX_VALUE;
for (Integer i : com) {
dist = Math.min(dist,
Math.abs(home[0] - chickens.get(i)[0]) + Math.abs(home[1] - chickens.get(i)[1]));
}
tmp += dist;
}
return tmp;
}
}