https://www.acmicpc.net/problem/7576
import sys
input = sys.stdin.readline
from collections import deque
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
n, m = map(int, input().split())
maps = [list(map(int, input().split())) for _ in range(m)]
ripenses = deque([[i, j] for i in range(m) for j in range(n) if maps[i][j] == 1])
while ripenses:
p = ripenses.popleft()
for k in range(4):
ny, nx = p[0] + dy[k], p[1] + dx[k]
if (-1 < ny < m) and (-1 < nx < n) and (maps[ny][nx] == 0):
maps[ny][nx] = maps[p[0]][p[1]] + 1
ripenses.append([ny, nx])
count = 0
for i in range(m): # y
for j in range(n): # x
if maps[i][j] == 0:
print(-1)
exit(0)
count = max(count, max(maps[i]))
print(count - 1)
'Coding Test > 백준' 카테고리의 다른 글
[백준] / [Python] / [7569] 토마토 (1) | 2024.03.14 |
---|---|
[백준] / [Python] / [10026] 적록색약 (1) | 2024.03.14 |
[백준] / [Python] / [1043] 거짓말 (0) | 2024.03.12 |
[백준] / [Python] / [20920] 영단어 암기는 괴로워 (0) | 2024.03.12 |
[백준] / [Python] / [26069] 붙임성 좋은 총총이 (0) | 2024.03.12 |