Skip to content

Commit 71fd31b

Browse files
authored
Create 7.py
1 parent e565443 commit 71fd31b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

12/7.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from itertools import combinations
2+
3+
n, m = map(int, input().split())
4+
chicken, house = [], []
5+
6+
for r in range(n):
7+
data = list(map(int, input().split()))
8+
for c in range(n):
9+
if data[c] == 1:
10+
house.append((r, c)) # 일반 집
11+
elif data[c] == 2:
12+
chicken.append((r, c)) # 치킨집
13+
14+
# 모든 치킨 집 중에서 m개의 치킨 집을 뽑는 조합 계산
15+
candidates = list(combinations(chicken, m))
16+
17+
# 치킨 거리의 합을 계산하는 함수
18+
def get_sum(candidate):
19+
result = 0
20+
# 모든 집에 대하여
21+
for hx, hy in house:
22+
# 가장 가까운 치킨 집을 찾기
23+
temp = 1e9
24+
for cx, cy in candidate:
25+
temp = min(temp, abs(hx - cx) + abs(hy - cy))
26+
# 가장 가까운 치킨 집까지의 거리를 더하기
27+
result += temp
28+
# 치킨 거리의 합 반환
29+
return result
30+
31+
# 치킨 거리의 합의 최소를 찾아 출력
32+
result = 1e9
33+
for candidate in candidates:
34+
result = min(result, get_sum(candidate))
35+
print(result)

0 commit comments

Comments
 (0)