Post

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

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

2469๋ฒˆ: ์น˜ํ‚จ ๋ฐฐ๋‹ฌ


2. ์ฝ”๋“œ

Python3 31120KB 680ms

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
[15686] ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

๐Ÿ’› ๋ฌธ์ œ
ํฌ๊ธฐ๊ฐ€ Nร—N์ธ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๋„์‹œ๋Š” 1ร—1ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค.
๋„์‹œ์˜ ๊ฐ ์นธ์€ ๋นˆ ์นธ, ์น˜ํ‚จ์ง‘, ์ง‘ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
๋„์‹œ์˜ ์นธ์€ (r, c)์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ ,
rํ–‰ c์—ด ๋˜๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ r๋ฒˆ์งธ ์นธ, ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ c๋ฒˆ์งธ ์นธ์„ ์˜๋ฏธํ•œ๋‹ค.
r๊ณผ c๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

์ด ๋„์‹œ์— ์‚ฌ๋Š” ์‚ฌ๋žŒ๋“ค์€ ์น˜ํ‚จ์„ ๋งค์šฐ ์ข‹์•„ํ•œ๋‹ค.
๋”ฐ๋ผ์„œ, ์‚ฌ๋žŒ๋“ค์€ "์น˜ํ‚จ ๊ฑฐ๋ฆฌ"๋ผ๋Š” ๋ง์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ์ง‘๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์ด๋‹ค.
์ฆ‰, ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ์ง‘์„ ๊ธฐ์ค€์œผ๋กœ ์ •ํ•ด์ง€๋ฉฐ, ๊ฐ๊ฐ์˜ ์ง‘์€ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” ๋ชจ๋“  ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์ด๋‹ค.

์ž„์˜์˜ ๋‘ ์นธ (r1, c1)๊ณผ (r2, c2) ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” |r1-r2| + |c1-c2|

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ ์ง€๋„๋ฅผ ๊ฐ–๋Š” ๋„์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž.
0 2 0 1 0
1 0 1 0 0
0 0 0 0 0
0 0 0 1 1
0 0 0 1 2
0์€ ๋นˆ ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘์ด๋‹ค.
(2, 1)์— ์žˆ๋Š” ์ง‘๊ณผ (1, 2)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |2-1| + |1-2| = 2,
(5, 5)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |2-5| + |1-5| = 7์ด๋‹ค.
๋”ฐ๋ผ์„œ, (2, 1)์— ์žˆ๋Š” ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” 2์ด๋‹ค.

(5, 4)์— ์žˆ๋Š” ์ง‘๊ณผ (1, 2)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |5-1| + |4-2| = 6,
(5, 5)์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” |5-5| + |4-5| = 1์ด๋‹ค.
๋”ฐ๋ผ์„œ, (5, 4)์— ์žˆ๋Š” ์ง‘์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š” 1์ด๋‹ค.

์ด ๋„์‹œ์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘์€ ๋ชจ๋‘ ๊ฐ™์€ ํ”„๋žœ์ฐจ์ด์ฆˆ์ด๋‹ค.
ํ”„๋ Œ์ฐจ์ด์ฆˆ ๋ณธ์‚ฌ์—์„œ๋Š” ์ˆ˜์ต์„ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ผ๋ถ€ ์น˜ํ‚จ์ง‘์„ ํ์—…์‹œํ‚ค๋ ค๊ณ  ํ•œ๋‹ค.
์˜ค๋žœ ์—ฐ๊ตฌ ๋์— ์ด ๋„์‹œ์—์„œ ๊ฐ€์žฅ ์ˆ˜์ต์„ ๋งŽ์ด ๋‚ผ ์ˆ˜ ์žˆ๋Š”  ์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ M๊ฐœ

๋„์‹œ์— ์žˆ๋Š” ์น˜ํ‚จ์ง‘ ์ค‘์—์„œ ์ตœ๋Œ€ M๊ฐœ๋ฅผ ๊ณ ๋ฅด๊ณ , ๋‚˜๋จธ์ง€ ์น˜ํ‚จ์ง‘์€ ๋ชจ๋‘ ํ์—…์‹œ์ผœ์•ผ ํ•œ๋‹ค.
์–ด๋–ป๊ฒŒ ๊ณ ๋ฅด๋ฉด, ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋ ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— N(2 โ‰ค N โ‰ค 50)๊ณผ M(1 โ‰ค M โ‰ค 13)์ด ์ฃผ์–ด์ง„๋‹ค.
๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋„์‹œ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๋„์‹œ์˜ ์ •๋ณด๋Š” 0, 1, 2๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , 0์€ ๋นˆ ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘
์ง‘์˜ ๊ฐœ์ˆ˜๋Š” 2N๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์ ์–ด๋„ 1๊ฐœ๋Š” ์กด์žฌํ•œ๋‹ค.
์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” M๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 13๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์„ ์ตœ๋Œ€ M๊ฐœ๋ฅผ ๊ณจ๋ž์„ ๋•Œ,
๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
"""

from itertools import combinations

# Nร—N์ธ ๋„์‹œ, ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘์„ ์ตœ๋Œ€ M๊ฐœ
N, M = map(int, input().split())

city = []
for i in range(N) :
    # 0์€ ๋นˆ ์นธ, 1์€ ์ง‘, 2๋Š” ์น˜ํ‚จ์ง‘
    city.append(list(map(int, input().split())))
house = []
chicken = []

for i in range(N) :
    for j in range(N) :
        # ์ง‘
        if city[i][j] == 1 :
            # [[0, 2], [1, 4], [2, 1], [3, 2]]
            house.append([i, j])
        # ์น˜ํ‚จ์ง‘
        elif city[i][j] == 2 :
            # [[1, 2], [2, 2], [4, 4]]
            chicken.append([i, j])

# ์กฐํ•ฉ์œผ๋กœ M๊ฐœ ๋ฝ‘๊ณ , ๊ฐ๊ฐ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ณ , ๋„์‹œ์˜ ์น˜ํ‚จ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Ÿ๊ฐ’ ๊ณ„์† ๊ฐฑ์‹ 
# [([1, 2], [2, 2], [4, 4])]
combi = list(combinations(chicken, M))
result = []

for com in combi :
    answer = 0
    for h in house :
        x1, y1 = h
        # ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด
        dist = int(1e9)

        for c in com :
            x2, y2 = c
            dist = min(dist, abs(x1-x2) + abs(y1-y2))
        answer += dist
    result.append(answer)

# ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘ ์กฐํ•ฉ ๋งˆ๋‹ค ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•œ ํ›„ ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•จ
print(min(result))


3. ํ•ด์„ค

1
ํ์—…์‹œํ‚ค์ง€ ์•Š์„ ์น˜ํ‚จ์ง‘ ์กฐํ•ฉ ๋งˆ๋‹ค ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ๊ตฌํ•œ ํ›„ ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•จ
This post is licensed under CC BY 4.0 by the author.