[BOJ] 15686. ์นํจ๋ฐฐ๋ฌ(python)
๐๋ฌธ์
๐ช์์ด๋์ด
m๊ฐ ์นํจ์ง ๊ณ ๋ฅด๊ธฐ
์์์ ์ค๋ณต์ด ์๋ combination์ ํตํด ์ ์ฒด ์นํจ์ง ์ค m๊ฐ์ ์นํจ์ง์ ๊ณ ๋ฅธ๋ค.์นํจ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
๊ฐ ์ง๋ง๋ค ๊ณ ๋ฅธ m๊ฐ์ ์นํจ์ง์์ ๊ฐ์ฅ ๊ฐ๊น์ด ์นํจ์ง๊ณผ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.๋์์ ์นํจ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
๐ฅ์ฝ๋
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
import sys; input = sys.stdin.readline
from itertools import combinations
def chickenRoad(house, chicken):
hx, hy = house
cx, cy = chicken
return abs(hx-cx) + abs(hy-cy)
houses = []
chickenShops = []
n, m = map(int, input().split())
for i in range(n):
town = input().split()
for j in range(n):
if town[j] == '1': houses.append((i+1,j+1))
elif town[j] == '2': chickenShops.append((i+1,j+1))
answer = float('inf')
for c in combinations(chickenShops,m):
combiRoad = 0
for h in houses:
streetPerHouse = float('inf')
for i in range(m):
streetPerHouse = min(streetPerHouse, chickenRoad(h,c[i]))
combiRoad += streetPerHouse
answer = min(answer,combiRoad)
print(answer)
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.