algorithm'''problem solve
[백준] 15686 - 치킨 배달 (구현, 브루트포스)
JunHwa Park
2020. 10. 2. 10:02
https://www.acmicpc.net/problem/15686
15686번: 치킨 배달
크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸
www.acmicpc.net
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
|
from itertools import combinations
n, m = map(int, input().split())
graph = [[0] for _ in range(n + 1)]
homes = list()
stores = list()
for i in range(1, n + 1):
graph[i] += list(map(int, input().split()))
for j in range(1, n + 1):
if graph[i][j] == 1:
homes.append((i, j))
elif graph[i][j] == 2:
stores.append((i, j))
answer = int(1e9)
combs = combinations(stores, m)
for comb in combs:
town_chicken = 0
for home in homes:
chicken = int(1e9)
for store in comb:
dist = abs(home[0] - store[0]) + abs(home[1] - store[1])
if chicken > dist:
chicken = dist
town_chicken += chicken
if answer > town_chicken:
answer = town_chicken
print(answer)
|
cs |
구현문제이다.
뭔가 효율적인 방법이 없을까 고민했지만, N과 M의 입력 크기 제한을 보니 그냥 브루트포스가 답인 것 같다.
파이썬의 경우에는 combinations를 사용해서 쉽게 풀 수 있다. 다른 언어로는 재귀로 풀어야 할 것 같다.