[BOJ][๐ŸŸก5][๋ฐฑ์ค€#15686] ์น˜ํ‚จ ๋ฐฐ๋‹ฌ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #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๊ฐœ๋ฅผ ๊ณจ๋ž์„ ๋•Œ, ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

5 3
0 0 1 0 0
0 0 2 0 1
0 1 2 0 0
0 0 1 0 0
0 0 0 0 2


์ถœ๋ ฅ

5


์˜ˆ์ œ 2

์ž…๋ ฅ

5 2
0 2 0 1 0
1 0 1 0 0
0 0 0 0 0
2 0 0 1 1
2 2 0 1 2


์ถœ๋ ฅ

10


์˜ˆ์ œ 3

์ž…๋ ฅ

5 1
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0
1 2 0 0 0


์ถœ๋ ฅ

11


์˜ˆ์ œ 4

์ž…๋ ฅ

5 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1
1 2 0 2 1


์ถœ๋ ฅ

32


My Sol

import sys
input = sys.stdin.readline

def pick(i, s, M):
    global chickens, chickensAlive
    global visited, ans
    if i == M:
        ans = min(ans, calcDistance(chickensAlive))
        return

    for k in range(s, chickensCnt):
        if not visited[k]:
            visited[k] = 1
            chickensAlive.append(chickens[k])
            pick(i+1, k+1, M)
            visited[k] = 0
            chickensAlive.pop()


def calcDistance(chickensAlive):
    global houses

    summin_d = 0
    for house in houses:
        hi, hj = house
        d_lst = []
        for chicken in chickensAlive:
            ci, cj = chicken
            d = abs(ci-hi) + abs(cj-hj)
            d_lst.append(d)
        summin_d += min(d_lst)

    return summin_d


N, M = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(N)]

chickens = []
chickensCnt = 0
houses = []
for i in range(N):
    for j in range(N):
        if mat[i][j]==1:
            houses.append((i, j))
        elif mat[i][j]==2:
            chickens.append((i, j))
            chickensCnt += 1

visited = [0]*chickensCnt
chickensAlive = []
ans = 1000000
pick(0, 0, M)
print(ans)

๋ณต์žกํ•ด๋ณด์ด์ง€๋งŒ ๋…ผ๋ฆฌ์  ํ๋ฆ„์„ ์ž˜ ๊ณ ๋ คํ•˜๋ฉด ์ถฉ๋ถ„ํžˆ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.


์ž…๋ ฅ ์ฒ˜๋ฆฌ ๋ฐ ๋ณ€์ˆ˜ ์ •์˜

N๊ณผ M, mat 2์ฐจ์› ๋ฐฐ์—ด์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๊ณ , mat์œผ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์–ป์–ด์„œ ์•„๋ž˜ ์ •์˜ํ•œ ๋ณ€์ˆ˜๋“ค์— ์ฑ„์›Œ๋„ฃ๋Š”๋‹ค. x, y ์ขŒํ‘œ๋ฅผ tuple๋กœ ์‚ฝ์ž…ํ•˜๊ณ , index๋Š” 0๋ถ€ํ„ฐ ๋ฐ›์•˜๋‹ค. ์–ด์ฐจํ”ผ distance๋ฅผ ๊ตฌํ•  ๋•Œ๋Š” ์ฐจ์ด๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฏ€๋กœ ๊ตณ์ด 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ํ•„์š”๋Š” ์—†๋‹ค.

๋„์‹œ์˜ ์น˜ํ‚จ์ง‘์˜ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ chickens์— ๋„ฃ์—ˆ๋‹ค. ๋„์‹œ์˜ ์ง‘์˜ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ houses์— ๋„ฃ์—ˆ๋‹ค. ์น˜ํ‚จ์ง‘์˜ ์ˆ˜๋ฅผ ์ฒ˜์Œ์— ์ œ์‹œํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดํ›„ ์™„์ „ ํƒ์ƒ‰์—์„œ ์ขŒํ‘œ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ ์น˜ํ‚จ์ง‘์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” chickensCnt๋ฅผ 0์œผ๋กœ ์ •์˜ํ•˜์—ฌ 1์”ฉ ์˜ฌ๋ ค์ค€๋‹ค.


์™„์ „ํƒ์ƒ‰

mat์˜ ๋ชจ๋“  ์ขŒํ‘œ์— ๋Œ€ํ•ด ๊ฐ’์„ ์กฐํšŒํ•œ๋‹ค. 1์ด๋ฉด houses์— ์ขŒํ‘œ๋ฅผ tuple๋กœ ์‚ฝ์ž…ํ•˜๊ณ , 2์ด๋ฉด chickens์— ์ขŒํ‘œ๋ฅผ tuple๋กœ ์‚ฝ์ž…ํ•œ๋‹ค. chickens์— ์‚ฝ์ž…ํ•  ๋•Œ๋งˆ๋‹ค chickensCnt๋ฅผ 1์”ฉ ์˜ฌ๋ ค์ค€๋‹ค.


์žฌ๊ท€์™€ ์กฐํ•ฉ : pick()

M๊ฐœ์˜ ์น˜ํ‚จ์ง‘์„ ์‚ด๋ฆฌ๋ ค ํ•  ๋•Œ, ์ „์ฒด ์น˜ํ‚จ์ง‘์˜ ์ง‘ํ•ฉ์ธ chickens์˜ ๊ฐ ์ขŒํ‘œ๋งˆ๋‹ค M๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ด๋Š” ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์กฐํ•ฉ์ด๋ฉฐ, ์ด๋ฅผ ์žฌ๊ท€์ธ pick() ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. dfs์™€ stack์— ์ž„์˜์˜ ์น˜ํ‚จ์ง‘์„ ์„ ํƒํ•˜์—ฌ ์ขŒํ‘œ๋ฅผ ๋„ฃ์œผ๋ฉฐ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•œ M๊ฐœ์˜ ์น˜ํ‚จ์ง‘ ์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋‚ธ๋‹ค.

์ด ๊ฐ๊ฐ์˜ ์ƒํ™ฉ๋งˆ๋‹ค ์น˜ํ‚จ์ง‘์˜ ๊ฐœ์ˆ˜๊ฐ€ M์ผ ๋•Œ์˜ ์ž„์˜์˜ ์‚ด์•„์žˆ๋Š” ์น˜ํ‚จ์ง‘ ์ขŒํ‘œ๋ฅผ chickensAlive๋ฅผ ์ •์˜ํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.


๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ : calcDistance()

๋ชจ๋“  ์ƒํ™ฉ์˜ chickensAlive์— ๋Œ€ํ•˜์—ฌ pick() ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ calcDistance() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ธ์ž๋กœ๋Š” chickensAlive ์ง‘ํ•ฉ์„ ๋ฐ›๋Š”๋‹ค.

์ด ํ•จ์ˆ˜๋Š” houses์˜ ๊ฐ ์ขŒํ‘œ๋“ค์— ๋Œ€ํ•˜์—ฌ chickensAlive์˜ ๊ฐ ์น˜ํ‚จ์ง‘์„ ๋ชจ๋‘ ์ˆœํšŒํ•ด ์ง‘์ง‘๋งˆ๋‹ค ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋”ํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด ๋ฐ˜ํ™˜๊ฐ’์€ pick() ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ „์—ญ๋ณ€์ˆ˜์ธ ans์™€ ๋น„๊ตํ•ด ๋” ์ž‘์€ ๊ฐ’์„ ans์— ์ €์žฅํ•œ๋‹ค. ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋ฉด ์ด ans๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ