import sys
from collections import deque
input = sys.stdin.readline
def bfs(x, y):
q = deque([(x, y)])
visited[x][y] = 1
seaList = []
while q:
x, y = q.popleft()
sea = 0
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if not graph[nx][ny]:
sea += 1
elif graph[nx][ny] and not visited[nx][ny]:
q.append((nx, ny))
visited[nx][ny] = 1
if sea > 0:
seaList.append((x, y, sea))
for x, y, sea in seaList:
graph[x][y] = max(0, graph[x][y] - sea)
return 1
n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
ice = []
for i in range(n):
for j in range(m):
if graph[i][j]:
ice.append((i, j))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
year = 0
while ice:
visited = [[0] * m for _ in range(n)]
delList = []
group = 0
for i, j in ice:
if graph[i][j] and not visited[i][j]:
group += bfs(i, j)
if graph[i][j] == 0:
delList.append((i, j))
if group > 1:
print(year)
break
ice = sorted(list(set(ice) - set(delList)))
year += 1
if group < 2:
print(0)
'Algorithm > Python' 카테고리의 다른 글
[Algorithm|Python] 백준 1051번 / 99클럽 12일차 TIL (0) | 2025.02.05 |
---|---|
[Algorithm|Python] 백준 1018번 / 99클럽 코테 스터디 11일차 TIL (0) | 2025.02.04 |
[Algorithm|Python] 백준 2776번 / 99클럽 코테 스터디 9일차 TIL (0) | 2025.01.23 |
[Algorithm|Python] 백준 1260번 / 99클럽 코테 스터디 8일차 TIL (0) | 2025.01.22 |
[Algorithm|Python] 백준 1260번 / 99클럽 코테 스터디 6일차 TIL (0) | 2025.01.21 |