Skip to content

Commit 0b125c4

Browse files
authored
Merge pull request #1091 from AlgorithmWithGod/zinnnn37
[20251011] BOJ / G5 / 치킨 배달 / 김민진
2 parents 40ae59c + 43c8f6c commit 0b125c4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
```java
2+
import java.awt.*;
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_15686_치킨_배달 {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
private static StringTokenizer st;
13+
14+
private static int N, M, ans, chickenCnt;
15+
private static int[][] map;
16+
private static int[] selected;
17+
18+
private static List<Point> houses;
19+
private static List<Point> chickens;
20+
21+
public static void main(String[] args) throws IOException {
22+
init();
23+
sol(0, 0);
24+
25+
bw.write(ans + "");
26+
bw.flush();
27+
bw.close();
28+
br.close();
29+
}
30+
31+
private static void init() throws IOException {
32+
st = new StringTokenizer(br.readLine());
33+
N = Integer.parseInt(st.nextToken());
34+
M = Integer.parseInt(st.nextToken());
35+
ans = Integer.MAX_VALUE;
36+
37+
chickenCnt = 0;
38+
map = new int[N][N];
39+
houses = new ArrayList<>();
40+
chickens = new ArrayList<>();
41+
42+
for (int i = 0; i < N; i++) {
43+
st = new StringTokenizer(br.readLine());
44+
for (int j = 0; j < N; j++) {
45+
map[i][j] = Integer.parseInt(st.nextToken());
46+
47+
if (map[i][j] == 1) {
48+
houses.add(new Point(i, j));
49+
} else if (map[i][j] == 2) {
50+
chickens.add(new Point(i, j));
51+
chickenCnt++;
52+
}
53+
}
54+
}
55+
selected = new int[M];
56+
}
57+
58+
private static void sol(int depth, int start) throws IOException {
59+
if (depth == M) {
60+
measure();
61+
return;
62+
}
63+
64+
for (int i = start; i < chickenCnt; i++) {
65+
selected[depth] = i;
66+
sol(depth + 1, i + 1);
67+
}
68+
}
69+
70+
private static void measure() {
71+
int min = 0;
72+
for (Point h : houses) {
73+
int tmp = Integer.MAX_VALUE;
74+
for (int survived : selected) {
75+
Point c = chickens.get(survived);
76+
77+
tmp = Math.min(tmp, Math.abs(h.x - c.x) + Math.abs(h.y - c.y));
78+
}
79+
min += tmp;
80+
}
81+
ans = Math.min(ans, min);
82+
}
83+
84+
}
85+
```

0 commit comments

Comments
 (0)