forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7.py
More file actions
36 lines (30 loc) ยท 1.01 KB
/
7.py
File metadata and controls
36 lines (30 loc) ยท 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from itertools import combinations
n, m = map(int, input().split())
chicken, house = [], []
for r in range(n):
data = list(map(int, input().split()))
for c in range(n):
if data[c] == 1:
house.append((r, c)) # ์ผ๋ฐ ์ง
elif data[c] == 2:
chicken.append((r, c)) # ์นํจ์ง
# ๋ชจ๋ ์นํจ ์ง ์ค์์ m๊ฐ์ ์นํจ ์ง์ ๋ฝ๋ ์กฐํฉ ๊ณ์ฐ
candidates = list(combinations(chicken, m))
# ์นํจ ๊ฑฐ๋ฆฌ์ ํฉ์ ๊ณ์ฐํ๋ ํจ์
def get_sum(candidate):
result = 0
# ๋ชจ๋ ์ง์ ๋ํ์ฌ
for hx, hy in house:
# ๊ฐ์ฅ ๊ฐ๊น์ด ์นํจ ์ง์ ์ฐพ๊ธฐ
temp = 1e9
for cx, cy in candidate:
temp = min(temp, abs(hx - cx) + abs(hy - cy))
# ๊ฐ์ฅ ๊ฐ๊น์ด ์นํจ ์ง๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ํ๊ธฐ
result += temp
# ์นํจ ๊ฑฐ๋ฆฌ์ ํฉ ๋ฐํ
return result
# ์นํจ ๊ฑฐ๋ฆฌ์ ํฉ์ ์ต์๋ฅผ ์ฐพ์ ์ถ๋ ฅ
result = 1e9
for candidate in candidates:
result = min(result, get_sum(candidate))
print(result)