문제풀이/백준(Boj) 문제풀이

[백준][BFS] 7576. 토마토 (파이썬/Python)

얄루몬 2021. 11. 9. 15:01

from collections import deque
m,n = map(int,input().split())
graph = []
queue = deque([])
for i in range(n):
    graph.append(list(map(int,input().split())))
    
    for j in range(m): 
        if graph[i][j]==1:
            queue.append([i,j])
            
dx = [0,0,-1,1]
dy = [-1,1,0,0]
def bfs():
    while queue:
        x,y = queue.popleft()
        
        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]
            if 0 <= nx < n and 0 <= ny < m and graph[nx][ny]==0:
                queue.append([nx,ny])
                graph[nx][ny] = graph[x][y]+1
bfs()
result = 0
for i in graph:
    for j in i:
        if j==0:
            print(-1)
            exit(0)
    result = max(result,max(i))
print(result-1)