๐น ์นํจ ๋ฐฐ๋ฌ
1. ๋ฌธ์ ๋งํฌ
2. ์ฝ๋
Python3
31120KB
356ms
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
N, M = map(int, input().split())
city = []
chicken = []
home = []
for i in range(N):
city.append(list(map(int, input().split())))
for i in range(N):
for j in range(N):
if city[i][j] == 2:
chicken.append((i, j))
elif city[i][j] == 1:
home.append((i, j))
from itertools import combinations
def chicken_distance(c):
total_distance = 0
for h in home:
hx, hy = h
min_d = 2 * N
for t in c:
cx, cy = t
d = abs(cx - hx) + abs(cy - hy)
min_d = min(min_d, d)
total_distance += min_d
return total_distance
result = int(1e9)
for c in combinations(chicken, M):
distance = chicken_distance(c)
result = min(result, distance)
print(result)
3. ํด์ค
์กฐํฉ์ผ๋ก M๊ฐ์ ์นํจ์ง ์์น๋ฅผ ์ ํํ ํ ๊ฐ ์ง๋ง๋ค ๊ฐ์ฅ ๊ฐ๊น์ด ์นํจ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋๋ค.
๊ฐ ๊ฒฝ์ฐ๋ณ ๋์์ ์นํจ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ํ ๊ทธ ์ค ์ต์์ธ ๊ฒฝ์ฐ์ ์นํจ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ค.
This post is licensed under CC BY 4.0 by the author.