https://www.acmicpc.net/problem/7576
from collections import deque
m,n = map(int,input().split())
box = [list(map(int,input().split())) for _ in range(n)]
ripe = deque()
for i in range(n):
for j in range(m):
if box[i][j] == 1:
ripe.append((i,j))
dx = [0,1,0,-1]
dy = [1,0,-1,0]
def bfs(m,n,box):
days = -1
while ripe:
days += 1
for _ in range(len(ripe)):
x,y = ripe.popleft()
for i in range(4):
nx,ny = x+dx[i],y+dy[i]
if 0<=nx<n and 0<=ny<m and box[nx][ny]==0:
box[nx][ny] = box[x][y] + 1
ripe.append((nx,ny))
for row in box:
if 0 in row:
return -1
return days
print(bfs(m,n,box))
'알고리즘' 카테고리의 다른 글
백준 2583번 : 영역 구하기(python) (0) | 2021.07.26 |
---|---|
백준 2667번 : 단지번호붙이기(python) (0) | 2021.07.26 |
백준 1600번 : 말이 되고픈 원숭이(Python) (0) | 2021.06.20 |
programmers 두 개 뽑아서 더하기(Python) (0) | 2021.06.12 |
programmers 완주하지 못한 선수(Python) (0) | 2021.06.09 |