[BOJ][๐ŸŸก5][๋ฐฑ์ค€#14502] ์—ฐ๊ตฌ์†Œ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #14502


๋ฌธ์ œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ Nร—M์ธ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ง์‚ฌ๊ฐํ˜•์€ 1ร—1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ๋นˆ ์นธ, ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ฒฝ์€ ์นธ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋“ ์ฐจ์ง€ํ•œ๋‹ค.ย  ์ผ๋ถ€ ์นธ์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉฐ, ์ด ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ๋นˆ ์นธ์œผ๋กœ ๋ชจ๋‘ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฉฐ, ๊ผญ 3๊ฐœ๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ๊ตฌ์†Œ๊ฐ€ ์ƒ๊ธด ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

2 0 0 0 1 1 0 0 0 1 0 1 2 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 ์ด๋•Œ, 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ณณ์ด๋‹ค. ์•„๋ฌด๋Ÿฐ ๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ๋ชจ๋“  ๋นˆ ์นธ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. 2ํ–‰ 1์—ด, 1ํ–‰ 2์—ด, 4ํ–‰ 6์—ด์— ๋ฒฝ์„ ์„ธ์šด๋‹ค๋ฉด ์ง€๋„์˜ ๋ชจ์–‘์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง€๊ฒŒ ๋œ๋‹ค.

2 1 0 0 1 1 0 1 0 1 0 1 2 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ๋ชจ์Šต์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง„๋‹ค.

2 1 0 0 1 1 2 1 0 1 0 1 2 2 0 1 1 0 1 2 2 0 1 0 0 0 1 2 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 ๋ฒฝ์„ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์œ„์˜ ์ง€๋„์—์„œ ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋Š” 27์ด๋‹ค. ์—ฐ๊ตฌ์†Œ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (3 โ‰ค N, M โ‰ค 8) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„์˜ ๋ชจ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜์ด๋‹ค. 2์˜ ๊ฐœ์ˆ˜๋Š” 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ ์ด์ƒ์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

7 7
2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0


์ถœ๋ ฅ

27


์˜ˆ์ œ 2

์ž…๋ ฅ

4 6
0 0 0 0 0 0
1 0 0 0 0 2
1 1 1 0 0 2
0 0 0 0 0 2


์ถœ๋ ฅ

9


์˜ˆ์ œ 3

์ž…๋ ฅ

8 8
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
2 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0


์ถœ๋ ฅ

3


My Sol

import sys
input = sys.stdin.readline


def wallsPick(i, start):
    global spaces, walls
    global cntZero

    if i == 3:
        mat2 = makeWalls(walls)
        runVirus(mat2)
        return

    for k in range(start, cntZero):
        if not visited[k]:
            visited[k] = 1
            walls.append(spaces[k])
            wallsPick(i+1, k+1)
            visited[k] = 0
            walls.pop()


def makeWalls(walls):
    mat2 = [lst[::] for lst in mat]
    for wall in walls:
        wi, wj = wall
        mat2[wi][wj] = 1
    return mat2


def runVirus(mat2):
    global viruses, H, W
    global cntZero, ans

    didj = [(-1,0), (1,0), (0,-1), (0,1)]

    virusesQ = viruses[::]
    cntZero2 = cntZero - 3
    start = -1
    end = len(virusesQ)

    while start < end - 1:
        start += 1
        vi, vj = virusesQ[start]

        for di, dj in didj:
            if 0<=vi+di<H and 0<=vj+dj<W:
                if not mat2[vi+di][vj+dj]:
                    mat2[vi+di][vj+dj] = 2
                    cntZero2 -= 1
                    virusesQ.append((vi+di, vj+dj))
                    end += 1

    ans = max(cntZero2, ans)


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

# ๋ฐ”์ด๋Ÿฌ์Šค์™€ ๋นˆ ๊ณต๊ฐ„ ์œ„์น˜ ํ™•์ธ
viruses = []
spaces = []
cntZero = 0
for i in range(H):
    for j in range(W):
        if mat[i][j]==2:
            viruses.append((i, j))
        elif not mat[i][j]:
            spaces.append((i, j))
            cntZero += 1

# main ํ•จ์ˆ˜ ์‹คํ–‰์„ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ •์˜
walls = []
visited = [0]*cntZero
ans = 0

# main ํ•จ์ˆ˜ ์‹คํ–‰
wallsPick(0, 0)
print(ans)


์ž…๋ ฅ ์ฒ˜๋ฆฌ

๊ฐ€๋กœ, ์„ธ๋กœ, ์ง€๋„๋ฅผ ๊ฐ๊ฐ W, H, map์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์•„ ํ˜•์‹์— ๋งž๊ฒŒ ์ฒ˜๋ฆฌํ•œ๋‹ค.


์™„์ „ ํƒ์ƒ‰ : ๋ฐ”์ด๋Ÿฌ์Šค์™€ ๋นˆ ๊ณต๊ฐ„ ์œ„์น˜ ํ™•์ธ

์น˜ํ‚จ ๋ฐฐ๋‹ฌ ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋นˆ ๊ณต๊ฐ„๊ณผ ๋ฐ”์ด๋Ÿฌ์Šค ํ‘œ์‹œ๋ฅผ ๊ฐ๊ฐ 0๊ณผ 2๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ขŒํ‘œ๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€๋‹ค. ์ด๋•Œ 0์˜ ๊ฐœ์ˆ˜๋Š” ๋‚˜์ค‘์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ „ํŒŒ๋œ ๋’ค์— ๋”ฐ๋กœ ์„ธ์ง€ ์•Š๊ณ  ์ฒ˜์Œ์— ์„ธ์–ด์ค€ ๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ „ํŒŒ๋  ๋•Œ๋งˆ๋‹ค 1์„ ๋นผ์ฃผ์–ด ๋‚˜์ค‘์— ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†๋„๋ก ํ•œ๋‹ค.


DFS / STACK / ์กฐํ•ฉ / ๋ธŒ๋ฃจํŠธํฌ์Šค : 3๊ฐœ์˜ ๋ฒฝ ์ขŒํ‘œ ์„ ํƒ

0์˜ ์ง‘ํ•ฉ์ธ spaces์˜ ๊ฐ ์ขŒํ‘œ๋“ค ์ค‘์—์„œ 3๊ฐœ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ํ•จ์ˆ˜๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. DFS๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜๋Š” wallsPick() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์˜€์œผ๋ฉฐ, ์„ธ๋ถ€ ๋™์ž‘ ํ•จ์ˆ˜๋“ค์„ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งˆ๋‹ค ๋ฐ˜๋ณต์‹œํ‚ค๋Š” mainํ•จ์ˆ˜์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ๊ฐ€์žฅ ํฐ ์˜๋ฏธ๋Š” ์ด๋ฆ„์ฒ˜๋Ÿผ 3๊ฐœ์˜ ๋ฒฝ์˜ ์ขŒํ‘œ๋ฅผ ์„ ํƒํ•ด ๋ฐฐ์—ด์— ๋„ฃ๋Š” ๊ฒƒ์— ์žˆ๋‹ค.


makeWalls() ํ•จ์ˆ˜

wallsPick์˜ ๋ชจ๋“  walls์— ๋Œ€ํ•ด ์ „๋‹ฌ๋ฐ›์•„ walls ๋‚ด์˜ 3๊ฐœ์˜ ๋ฒฝ ์ขŒํ‘œ๋ฅผ mat์— ๋Œ€ํ•˜์—ฌ 1๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ mat์€ ์ „์—ญ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์ด ์ž์ฒด์— ์กฐ์ž‘์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—์„œ mat์„ ํ™œ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— mat2๋ฅผ ๋”ฐ๋กœ ์ •์˜ํ•ด์„œ ์ด๋ฅผ ์กฐ์ž‘ํ•œ ๋’ค, ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.


BFS / QUEUE : runVirus() ํ•จ์ˆ˜

makeWalls() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ป์–ด๋‚ธ 3๊ฐœ์˜ ๋ฒฝ์„ ๋ฐ˜์˜ํ•œ mat2 2์ฐจ์› ๋ฐฐ์—ด์„ ์ „๋‹ฌ๋ฐ›์•„ virus์˜ ํ™•์‚ฐ์„ ์‹œ์ž‘ํ•œ๋‹ค. ์ด๋ฏธ mat์„ ์ •์˜ํ•  ๋•Œ๋ถ€ํ„ฐ ์กฐ์‚ฌ๋œ virus์˜ ์ง‘ํ•ฉ viruses๋ฅผ Queue๋กœ ์‚ฌ์šฉํ•˜์—ฌ BFS๋ฅผ ์‹ค์‹œํ•œ๋‹ค. ์ด๋•Œ viruses ์—ญ์‹œ mat ์ฒ˜๋Ÿผ ์ „์—ญ๋ณ€์ˆ˜ ๋ฐฐ์—ด์ด๋ฏ€๋กœ ์ด๋ฅผ ์ง์ ‘ ์กฐ์ž‘ํ•˜์ง€ ์•Š๊ณ , virusesQ๋กœ deepcopyํ•˜์—ฌ ์กฐ์ž‘์„ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— 0์„ ๋”ฐ๋กœ ์„ธ์–ด์ฃผ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ์ „์—ญ๋ณ€์ˆ˜ cntZero์—์„œ 3์„ ๋บ€ cntZero2๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค. 3์„ ๋บ€ ์ด์œ ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜ 3๊ฐœ๊ฐ€ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Queue์ด๋ฏ€๋กœ start์™€ end๋ฅผ ๋”ํ•ด๊ฐ€๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ ์ขŒํ‘œ๋ฅผ Queue์— ๋„ฃ๊ณ  ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ตœ๋Œ€ํ•œ์˜ ํ™•์‚ฐ์„ mat2์— ๋ฐ˜์˜ํ•œ๋‹ค. ํ•œ ๋ฒˆ ํ™•์‚ฐ์ด ๋  ๋•Œ๋งˆ๋‹ค cntZero2๋ฅผ 1์”ฉ ์ค„์—ฌ์ฃผ์–ด start==end๊ฐ€ ๋˜๋Š”, ์ฆ‰ Queue๊ฐ€ ๋น„๊ฒŒ ๋˜๋Š” ์ƒํ™ฉ์—์„œ cntZero2๋ฅผ ํ™•์ธํ•ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค.

0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ์ „์—ญ๋ณ€์ˆ˜์ธ ans์™€ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์„ ans์— ์ €์žฅํ•˜๋Š”๋ฐ, ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๊ฐ€์žฅ ํฐ ans๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

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

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