코테 대비 python/백준

15686 치킨 배달

ylab 2022. 8. 11. 13:50

https://www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸

www.acmicpc.net

#일단 좌표값 다 얻은다음
n,m = map(int,input().split())
a=[]
a_house=[]
a_chicken=[]
for i in range(n):
    b=list(map(int,input().split()))
    a.append(b)
    for j in range(len(b)):
        if b[j]==1:
            a_house.append((i+1,j+1))
        elif b[j]==2:
            a_chicken.append((i+1,j+1))

#print(a_house)
#print(a_chicken) 
#모든 경우의 수 따지기 즉 치킨집 M개를 선택하였을 때 제일 작은거 

from itertools import combinations

result=[]
for chicken in combinations(a_chicken,m):
    
    tmp=[]
    for house in a_house:
        mid=[]
        for j in range(m):
            chicken_len = abs(house[0] - chicken[j][0]) + abs(house[1] - chicken[j][1])
            mid.append(chicken_len)
        mid2=min(mid)
        tmp.append(mid2)
    tmp=sum(tmp)    
    result.append(tmp)
    
result = min(result)
print(result)