Post

๐Ÿน ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

1. ๋ฌธ์ œ ๋งํฌ

15686๋ฒˆ: ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ


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.