문제
https://www.acmicpc.net/problem/15686
코드
from itertools import combinations
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
city = []
chicken = []
house = []
def CalculateDistance(chicken):
value= 0
for i in house:
nx,ny = i
distance = int(1e9)
for j in chicken:
x,y = j
distance = min(distance,abs(nx-x) + abs(ny-y))
value += distance
return value
for i in range(n):
lst = list(map(int,input().split()))
city.append(lst)
for j in range(n):
if lst[j] == 1:
house.append((i,j))
if lst[j] == 2:
chicken.append((i,j))
lst = combinations(chicken, m)
answer = int(1e9)
for i in lst:
answer = min(answer,CalculateDistance(i))
print(answer)
'Algorithm > Backjoon' 카테고리의 다른 글
[백준] 23881 파이썬 - 선택 정렬 1 (1) | 2024.09.17 |
---|---|
[백준] 2583 파이썬 - 영역 구하기 (0) | 2024.09.09 |
[백준] 18405번 파이썬 - 경쟁적 전염 (0) | 2024.08.27 |
[백준] 12015번 파이썬 - 가장 긴 증가하는 부분 수열 2 (0) | 2024.06.27 |
[백준] 1095번 파이썬 - 마법의 구슬 (0) | 2024.05.10 |