Skip to content

Commit bc7308a

Browse files
committed
[Simulation] baekjoon-15686
1 parent b49006d commit bc7308a

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
| 29 | | [Baekjoon-17406 배열 돌리기 4](./src/Simulation/P17406) | |
6969
| 30 | | [SWEA-9760 Poker Game](./src/Simulation/swea9760) | |
7070
| 31 | | [SWEA-2115 벌꿀채취](./src/Simulation/swea2115) | |
71+
| 32 | | [Baekjoon-15686 치킨 배달](./src/Simulation/P15686) | |
7172

7273
## String
7374

Diff for: src/Simulation/P15686/Main.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package Simulation.P15686;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, M, ans = Integer.MAX_VALUE;
9+
static int[][] map;
10+
static List<Pos> home, chicken;
11+
static int[] selected;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Simulation/P15686/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
21+
map = new int[N][N];
22+
home = new ArrayList<>();
23+
chicken = new ArrayList<>();
24+
for (int i = 0; i < N; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
for (int j = 0; j < N; j++) {
27+
map[i][j] = Integer.parseInt(st.nextToken());
28+
if (map[i][j] == 1) home.add(new Pos(i, j));
29+
else if (map[i][j] == 2) chicken.add(new Pos(i, j));
30+
}
31+
}
32+
33+
selected = new int[M];
34+
dfs(0, 0);
35+
36+
System.out.println(ans);
37+
}
38+
39+
static void dfs(int start, int cnt) {
40+
if (cnt == M) {
41+
int sum = 0;
42+
for (Pos h : home) {
43+
int dist = Integer.MAX_VALUE;
44+
for (int s : selected) {
45+
Pos c = chicken.get(s);
46+
dist = Math.min(dist, Math.abs(h.i - c.i) + Math.abs(h.j - c.j));
47+
}
48+
sum += dist;
49+
}
50+
ans = Math.min(ans, sum);
51+
return;
52+
}
53+
54+
for (int i = start, size = chicken.size(); i < size; i++) {
55+
selected[cnt] = i;
56+
dfs(i+1, cnt+1);
57+
}
58+
}
59+
60+
static class Pos {
61+
int i, j;
62+
63+
public Pos(int i, int j) {
64+
this.i = i;
65+
this.j = j;
66+
}
67+
}
68+
}

Diff for: src/Simulation/P15686/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-15686] 치킨 배달
2+
3+
![image](https://user-images.githubusercontent.com/22045163/115846031-76158380-a45c-11eb-838c-6db02c4c81ac.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/115846082-80d01880-a45c-11eb-8116-5da572d0c834.png)

Diff for: src/Simulation/P15686/input.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5 1
2+
1 2 0 2 1
3+
1 2 0 2 1
4+
1 2 0 2 1
5+
1 2 0 2 1
6+
1 2 0 2 1

0 commit comments

Comments
 (0)