목차

치킨 배달

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()