알고리즘/백준
[BOJ/백준] 15686 치킨 배달 파이썬
개발싫어
2021. 9. 9. 14:01
728x90
문제 링크 : https://www.acmicpc.net/problem/15686
15686번: 치킨 배달
크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸
www.acmicpc.net
치킨집을 m개를 고르고 각 집마다 가장 가까운 치킨집과의 거리를 계산해서 합의 최솟값을 구하는 문제이다.
조합으로 치킨집을 m개 고르고 집마다 치킨집과의 거리를 계산해서 최솟값을 구하면 된다.
from sys import stdin
from itertools import combinations
input = stdin.readline
n,m = map(int, input().split())
board = []
home = []
chicken = []
for x in range(n):
board.append(input().strip().split())
for y in range(n):
if board[x][y] == '1':
home.append((x,y))
elif board[x][y] == '2':
chicken.append((x,y))
def solv():
answer = 9876543210
for comb in combinations(chicken,m):
total = 0
for sx,sy in home:
length = 9876543210
for ex,ey in comb:
length = min(length,abs(sx-ex)+abs(sy-ey))
total += length
answer = min(answer,total)
print(answer)
solv()