Skip to content

Commit 729c3ef

Browse files
committed
[Gold V] Title: 치킨 배달, Time: 172 ms, Memory: 15000 KB -BaekjoonHub
1 parent 3fd580f commit 729c3ef

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [Gold V] 치킨 배달 - 15686
2+
3+
[문제 링크](https://www.acmicpc.net/problem/15686)
4+
5+
### 성능 요약
6+
7+
메모리: 15000 KB, 시간: 172 ms
8+
9+
### 분류
10+
11+
구현, 브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2026년 3월 30일 15:20:21
16+
17+
### 문제 설명
18+
19+
<p>크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸, 왼쪽에서부터 c번째 칸을 의미한다. r과 c는 1부터 시작한다.</p>
20+
21+
<p>이 도시에 사는 사람들은 치킨을 매우 좋아한다. 따라서, 사람들은 "<strong>치킨 거리</strong>"라는 말을 주로 사용한다. <strong>치킨 거리</strong>는 집과 가장 가까운 치킨집 사이의 거리이다. 즉, 치킨 거리는 집을 기준으로 정해지며, 각각의 집은 <strong>치킨 거리</strong>를 가지고 있다. <strong>도시의 치킨 거리</strong>는 모든 집의 <strong>치킨 거리</strong>의 합이다.</p>
22+
23+
<p>임의의 두 칸 (r<sub>1</sub>, c<sub>1</sub>)과 (r<sub>2</sub>, c<sub>2</sub>) 사이의 거리는 |r<sub>1</sub>-r<sub>2</sub>| + |c<sub>1</sub>-c<sub>2</sub>|로 구한다.</p>
24+
25+
<p>예를 들어, 아래와 같은 지도를 갖는 도시를 살펴보자.</p>
26+
27+
<pre>0 2 0 1 0
28+
1 0 1 0 0
29+
0 0 0 0 0
30+
0 0 0 1 1
31+
0 0 0 1 2
32+
</pre>
33+
34+
<p>0은 빈 칸, 1은 집, 2는 치킨집이다.</p>
35+
36+
<p>(2, 1)에 있는 집과 (1, 2)에 있는 치킨집과의 거리는 |2-1| + |1-2| = 2, (5, 5)에 있는 치킨집과의 거리는 |2-5| + |1-5| = 7이다. 따라서, (2, 1)에 있는 집의 치킨 거리는 2이다.</p>
37+
38+
<p>(5, 4)에 있는 집과 (1, 2)에 있는 치킨집과의 거리는 |5-1| + |4-2| = 6, (5, 5)에 있는 치킨집과의 거리는 |5-5| + |4-5| = 1이다. 따라서, (5, 4)에 있는 집의 치킨 거리는 1이다.</p>
39+
40+
<p>이 도시에 있는 치킨집은 모두 같은 프랜차이즈이다. 프렌차이즈 본사에서는 수익을 증가시키기 위해 일부 치킨집을 폐업시키려고 한다. 오랜 연구 끝에 이 도시에서 가장 수익을 많이 낼 수 있는 치킨집의 개수는 최대 M개라는 사실을 알아내었다.</p>
41+
42+
<p>도시에 있는 치킨집 중에서 최대 M개를 고르고, 나머지 치킨집은 모두 폐업시켜야 한다. 어떻게 고르면, <strong>도시의 치킨 거리</strong>가 가장 작게 될지 구하는 프로그램을 작성하시오.</p>
43+
44+
### 입력
45+
46+
<p>첫째 줄에 N(2 ≤ N ≤ 50)과 M(1 ≤ M ≤ 13)이 주어진다.</p>
47+
48+
<p>둘째 줄부터 N개의 줄에는 도시의 정보가 주어진다.</p>
49+
50+
<p>도시의 정보는 0, 1, 2로 이루어져 있고, 0은 빈 칸, 1은 집, 2는 치킨집을 의미한다. 집의 개수는 2N개를 넘지 않으며, 적어도 1개는 존재한다. 치킨집의 개수는 M보다 크거나 같고, 13보다 작거나 같다.</p>
51+
52+
### 출력
53+
54+
<p>첫째 줄에 폐업시키지 않을 치킨집을 최대 M개를 골랐을 때, 도시의 치킨 거리의 최솟값을 출력한다.</p>
55+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
import java.util.List;
6+
import java.util.ArrayList;
7+
8+
// 5 3
9+
// 0 0 1 0 0
10+
// 0 0 2 0 1
11+
// 0 1 2 0 0
12+
// 0 0 1 0 0
13+
// 0 0 0 0 2
14+
15+
// 5 2
16+
// 0 2 0 1 0
17+
// 1 0 1 0 0
18+
// 0 0 0 0 0
19+
// 2 0 0 1 1
20+
// 2 2 0 1 2
21+
22+
// 5 1
23+
// 1 2 0 2 1
24+
// 1 2 0 2 1
25+
// 1 2 0 2 1
26+
// 1 2 0 2 1
27+
// 1 2 0 2 1
28+
29+
public class Main {
30+
31+
static int[][] cityMap;
32+
static List<int[]> eachHousePosition;
33+
static List<int[]> chickenPosition;
34+
static int[][] output;
35+
static int totalChickenDistance = Integer.MAX_VALUE;
36+
37+
38+
public static void main(String[] args) throws IOException {
39+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
40+
StringTokenizer st = new StringTokenizer(br.readLine());
41+
int N = Integer.parseInt(st.nextToken());
42+
int M = Integer.parseInt(st.nextToken());
43+
cityMap = new int[N][N];
44+
eachHousePosition = new ArrayList<>();
45+
chickenPosition = new ArrayList<>();
46+
output = new int[M][2];
47+
48+
for (int i = 0; i < N; i++) {
49+
st = new StringTokenizer(br.readLine());
50+
for (int j = 0; j < N; j++) {
51+
int value = Integer.parseInt(st.nextToken());
52+
cityMap[i][j] = value;
53+
54+
if (value == 1) {
55+
eachHousePosition.add(new int[]{i, j});
56+
} else if (value == 2) {
57+
chickenPosition.add(new int[]{i, j});
58+
}
59+
}
60+
}
61+
62+
combination(0, 0, chickenPosition.size(), M);
63+
System.out.println(totalChickenDistance);
64+
65+
}
66+
67+
private static void combination(int start, int depth, int n, int r) {
68+
if (depth == r) {
69+
int totalDistance = calculateTotalChickenDistance(eachHousePosition, output);
70+
totalChickenDistance = Math.min(totalChickenDistance, totalDistance);
71+
return;
72+
}
73+
74+
for (int i = start; i < n; i++) {
75+
output[depth] = chickenPosition.get(i);
76+
combination(i + 1, depth + 1, n, r);
77+
}
78+
}
79+
80+
private static int calculateTotalChickenDistance(List<int[]> housePosition, int[][] output) {
81+
int totalDistance = 0;
82+
for (int[] house : housePosition) {
83+
int min = Integer.MAX_VALUE;
84+
for (int[] chickenShop : output) {
85+
min = Math.min(min, Math.abs(house[0] - chickenShop[0]) + Math.abs(house[1] - chickenShop[1]));
86+
}
87+
88+
totalDistance += min;
89+
}
90+
91+
return totalDistance;
92+
}
93+
}

0 commit comments

Comments
 (0)