๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ์—ฐ๊ตฌ์†Œ BOJ #14502

by seolhee2750 2022. 1. 30.
๋ฌธ์ œ

https://www.acmicpc.net/problem/14502

 

14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import copy

n, m = map(int, input().split())
space = [] # ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ง€๋„
virusSpace = [] # ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜

""" ์ž…๋ ฅ """
for i in range(n):
    space.append(list(map(lambda x: int(x), input().split())))
    for j in range(m):
        if space[i][j] == 2:
            virusSpace.append((i, j))

check = copy.deepcopy(space)
result = 0
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

""" ๋ฐ”์ด๋Ÿฌ์Šค ์ „ํŒŒ """
def dfs(x, y, check):
    if check[x][y] == 2:
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx < 0 or ny < 0 or nx >= n or ny >= m: continue

            if check[nx][ny] == 0:
                check[nx][ny] = 2
                dfs(nx, ny, check)

""" ๋ฒฝ ์„ธ์šฐ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ํƒ์ƒ‰ """
def makeWall(start, count):
    global result
    safe_count = 0

    if count == 3:
        check = copy.deepcopy(space)
        for i in virusSpace:
            dfs(i[0], i[1], check)

        for i in range(n):
            safe_count += check[i].count(0)
        result = max(result, safe_count)
        return

    else:
        for i in range(start, n*m):
            x = i // m
            y = i % m
            if space[x][y] == 0:
                space[x][y] = 1
                makeWall(i, count+1)
                space[x][y] = 0

makeWall(0, 0)
print(result)

๐Ÿ‘‰ ๋ธŒ๋ฃจํŠธํฌ์Šค/DFS ๋ฌธ์ œ๋กœ, ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ , ๊ฒฝ์šฐ๋งˆ๋‹ค ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ „ํŒŒ๋˜๋Š” ๊ฒƒ์„ ์ฒดํฌํ•ด์ฃผ์—ˆ๋‹ค.

  • makeWall ํ•จ์ˆ˜๋ฅผ ๋ณด๋ฉด, ๋ฒฝ์„ ์ด 3๊ฐœ ์„ธ์›Œ์•ผ ํ•˜๋ฏ€๋กœ count๊ฐ€ 3์ด ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋ฒฝ์„ ์„ธ์› ๋‹ค.
    else ์กฐ๊ฑด๋ฌธ์˜ for๋ฌธ์„ ํ†ตํ•ด ์ง€๋„์˜ ๋ชจ๋“  ์ž๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•ด์ฃผ์—ˆ๋Š”๋ฐ,
    ์ž๋ฆฌ๊ฐ€ 0์ธ ๊ณณ์„ ๋ฐœ๊ฒฌํ•˜๋ฉด ๋ฒฝ์„ ์„ธ์šด ํ›„ makeWall์„ ์žฌ๊ท€ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
    ์žฌ๊ท€ํ˜ธ์ถœํ•  ๋•Œ๋Š” ํƒ์ƒ‰ ์‹œ์ž‘ ์ ์„ i๋กœ ํ•ด์ฃผ๊ณ , count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ์—ˆ๊ณ ,
    ํ˜ธ์ถœ์ด ๋๋‚˜๋ฉด ๋ฒฝ์„ ์„ธ์› ๋˜ ์ž๋ฆฌ๋Š” ๋‹ค์‹œ 0์„ ์ €์žฅํ•˜์—ฌ ๋ณต๊ตฌ์‹œ์ผฐ๋‹ค.
    count๊ฐ€ 3์ด ๋˜์—ˆ์„ ๋•Œ๋Š” 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šด space ๋ฆฌ์ŠคํŠธ๋ฅผ check์— ๋„ฃ๊ณ ,
    ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ virusSpace ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉฐ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ณณ๋งˆ๋‹ค๋ฅผ
    ์‹œ์ž‘์ ์œผ๋กœ ์ƒ๊ฐํ•˜์—ฌ dfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์—ˆ๋‹ค.
    dfs ํ•จ์ˆ˜ ์‹คํ–‰์ด ์ข…๋ฃŒ๋˜๋ฉด 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ , result์™€ ๋น„๊ตํ•ด์„œ ํฐ ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค.
  • dfs ํ•จ์ˆ˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ž…๋ ฅ๋ฐ›์€ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „ํŒŒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ด๋‹ค.
    ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ , ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜์—ฌ
    0์ด ์ €์žฅ๋œ ์ž๋ฆฌ๋ฅผ 2๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ž๋ฆฌ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด dfs๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ์˜€์ง€๋งŒ ์ˆœ์ „ํžˆ ํŒŒ์ด์ฌ์ด๋ผ ํ‘ธ๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.
    ์ผ๋‹จ ๋‚˜๋Š” deepcopy๋ฅผ ๋ชฐ๋ผ์„œ ๊ทธ๊ฑธ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ์— ์•„์ฃผ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.
    ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€