ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 15686 |
문제명 | 치킨 배달 |
레벨 | 골드 5 |
분류 |
브루트포스 |
시간복잡도 | O(N^2 + C(K,M)*M*N) |
인풋사이즈 | N<=50, K<=13, M<=13 |
사용한 언어 | Python |
제출기록 | 29200KB / 220ms |
최고기록 | 112ms |
해결날짜 | 2021/10/08 |
"""Solution code for "BOJ 15686. 치킨 배달".
- Problem link: https://www.acmicpc.net/problem/15686
- Solution link: http://www.teferi.net/ps/problems/boj/15686
"""
import itertools
INF = float('inf')
def main():
N, M = [int(x) for x in input().split()]
all_chickens, houses = [], []
for r in range(N):
line = input()
for c, info in enumerate(line.split()):
if info == '1':
houses.append((r, c))
elif info == '2':
all_chickens.append((r, c))
dists = [] # dists[i][j] = dist from i-th house to j-th chicken store.
for hr, hc in houses:
dists.append([abs(hr - cr) + abs(hc - cc) for cr, cc in all_chickens])
min_dist = INF
for chickens in itertools.combinations(range(len(all_chickens)), M):
dist = 0
for dists_from_house in dists:
dist += min(dists_from_house[x] for x in chickens)
min_dist = min(min_dist, dist)
print(min_dist)
if __name__ == '__main__':
main()