본문 바로가기
  • KEEP HUSTLE!
파이썬 코테 준비

[백준 7576] 토마토 - Python(파이썬)

by 하수군 2021. 3. 9.
from collections import deque
import sys

M, N = map(int, sys.stdin.readline().split())
arr = []
my_deq = deque()
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
cnt = 0

for i in range(N):
    arr.append(list(map(int, sys.stdin.readline().split())))
    for j in range(M):
        if arr[i][j] == 1:
            cnt += 1
            my_deq.append((i, j))
        elif arr[i][j] == -1:
            cnt += 1

answer = -1

while my_deq:
    my_len = len(my_deq)
    for _ in range(my_len):
        x, y = my_deq.popleft()
        for i in range(4):
            new_x, new_y = x + dx[i], y + dy[i]
            if 0 <= new_x < N and 0 <= new_y < M and arr[new_x][new_y] == 0:
                cnt += 1
                arr[new_x][new_y] = 1
                my_deq.append((new_x, new_y))

    answer += 1

if cnt == M * N:
    print(answer)
else:
    print(-1)

댓글