BOJ - 2638 - 치즈

Updated:

import sys
from collections import deque
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def BFS(cheese, N, M):
    dq = deque()
    dq.append((0, 0))
    air = [[-1] * M for _ in range(N)]
    air[0][0] = 1

    while dq:
        x, y = dq.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < N and 0 <= ny < M:
                if air[nx][ny] == -1:
                    if cheese[nx][ny] >= 1: cheese[nx][ny] += 1
                    else:
                        air[nx][ny] = 1
                        dq.append((nx, ny))

def solution():
    N, M = map(int, sys.stdin.readline().split())
    cheese = []
    for _ in range(N):
        cheese.append(list(map(int, input().split(' '))))

    time = 0
    while True:
        BFS(cheese, N, M)
        check = False
        for i in range(N):
            for j in range(M):
                if cheese[i][j] >= 3:
                    cheese[i][j] = 0
                    check = True
                elif cheese[i][j] == 2:
                    cheese[i][j] = 1

        if not check: break
        else: time += 1
    print(time)

solution()

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

Categories:

Updated:

Leave a comment