ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

์—ฐ๊ตฌ์†Œ ์‹œ๋ฆฌ์ฆˆ

 

๋ฌธ์ œ

 

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

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์—ฐ๊ตฌ์‹ค์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๊ณ , ๋ฒฝ์„ 3๊ฐœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ๊ฐ€์žฅ ๋งŽ์€ ์•ˆ์ „์ง€๋Œ€๋ฅผ ํ™•๋ณดํ•œ ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” FloodFill๊ณผ ๊ฐ™์ด ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์ง€์ ์œผ๋กœ๋ถ€ํ„ฐ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ์ „ํŒŒ๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜์—ฌ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ „ํŒŒ๋  ๊ฒƒ์ด๋‹ค. ํ† ๋งˆํ†  ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์ ์€ ๋ฒฝ์„ ์„ธ์›Œ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • ๋ฐฑํŠธ๋ž™ํ‚น์„ ํ†ตํ•ด 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ๊ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งˆ๋‹ค, ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „ํŒŒ์‹œํ‚ค๊ณ  ์•ˆ์ „ ์ง€์—ญ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque
from copy import deepcopy


def visitable(x, y, set_wall):
    return 0 <= x < n and 0 <= y < m and not set_wall[x][y]


def bfs():
    set_wall = deepcopy(graph)
    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
    q = deque()

    for x in range(n):
        for y in range(m):
            if set_wall[x][y] == 2:
                q.append([x, y])

    while q:
        cur_x, cur_y = q.popleft()
        for x, y in dirs:
            next_x, next_y = cur_x + x, cur_y + y
            if visitable(next_x, next_y, set_wall):
                set_wall[next_x][next_y] = 2
                q.append([next_x, next_y])

    cnt = 0
    for is_virus in set_wall:
        cnt += is_virus.count(0)

    return cnt


def dfs(select):
    global answer

    if select == 3:
        answer = max(answer, bfs())
        return

    for x in range(n):
        for y in range(m):
            if not graph[x][y]:
                graph[x][y] = 1
                dfs(select + 1)
                graph[x][y] = 0


if __name__ == '__main__':
    answer = 0
    n, m = map(int, stdin.readline().split())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]
    dfs(0)
    print(answer)

 ๋Š๋ฆฐ ํŒŒ์ด์ฌ์œผ๋กœ๋Š” ์‹œ๊ฐ„ ์ดˆ๋‚ด์— ํ†ต๊ณผํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ PyPy3๋กœ ์ œ์ถœํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€