백준 15686과 동일
N * N 도시가 빈칸, 치킨집, 집으로 구성되어 있다.
도시, (r, c) 행, 열 , 1부터 시작
치킨거리 : 집과 가장 가까운 치킨집 사이의 거리
지도 표시 방법 : 0 빈칸, 1 집, 2 치킨집
목적
입력
출력

# 조합별 최소거리 측정 함수
def cal_distance(candidate):
result = 0
for x1, y1 in house :
temp = 1e9
for x2, y2 in candidate:
temp = min(temp, (abs(x1-x2)+ abs(y1-y2)))
result+=temp
return result
# 입력
n, m = map(int, input().split())
maps = [list(map(int, input().split())) for _ in range(n)]
# 집, 치킨집 좌표 모으기
house = []
chichen = []
for i in range(n):
for j in range(n):
if maps[i][j] == 1:
house.append((i,j))
elif maps[i][j] == 2:
chichen.append((i,j))
# 치킨집 조합 생성
from itertools import combinations
candidates = list(combinations(chichen, m))
# 최적거리
opt = 1e9
for candi in candidates:
opt = min(opt, cal_distance(candi))
print(opt)