-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBOJ_2573_dooyeong.py
60 lines (45 loc) · 1.46 KB
/
BOJ_2573_dooyeong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from sys import stdin
from collections import deque
def solution(N, M, board):
def melt():
def bfs(visited, i, j):
q = deque([(i, j)])
visited.add((i, j))
while q:
r, c = q.popleft()
cnt = 0
for d in dirs:
nr, nc = r + d[0], c + d[1]
if 0 <= nr < N and 0 <= nc < M:
if board[nr][nc] == 0:
cnt += 1
elif (nr, nc) not in visited:
visited.add((nr, nc))
q.append((nr, nc))
board[r][c] = [board[r][c], cnt]
visited = set()
cnt = 0
for i in range(N):
for j in range(M):
if board[i][j] != 0 and (i, j) not in visited:
bfs(visited, i, j)
cnt += 1
for i in range(N):
for j in range(M):
if board[i][j] != 0:
gap = board[i][j][0] - board[i][j][1]
board[i][j] = gap if gap > 0 else 0
return cnt
ans = 0
while 1:
cnt = melt()
if cnt == 0:
return 0
if cnt > 1:
break
ans += 1
return ans
N, M = map(int, stdin.readline().split())
board = [list(map(int, stdin.readline().split())) for _ in range(N)]
dirs = [(1, 0), (0, -1), (-1, 0), (0, 1)]
print(solution(N, M, board))