-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaekJoon2638.py
61 lines (47 loc) · 1.3 KB
/
BaekJoon2638.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
61
# BaekJoon2638.py
from collections import deque
N, M = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(N)]
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def check_cheeze_left():
global arr
for ls in arr:
for l in ls:
if l == 1:
return True
return False
def check_cheeze():
global N, M, dx, dy, arr
melting = [[0 for _ in range(M)] for _ in range(N)]
visited = [[False for _ in range(M)] for _ in range(N)]
queue = deque([[0, 0]])
visited[0][0] = True
while queue:
value = queue.popleft()
for i in range(4):
nx = value[0] + dx[i]
ny = value[1] + dy[i]
if nx < 0 or ny < 0 or nx >= N or ny >= M:
continue
if visited[nx][ny]:
continue
if arr[nx][ny] == 1:
melting[nx][ny] += 1
continue
visited[nx][ny] = True
queue.append([nx, ny])
return melting
def delete_cheeze(cheezes):
global arr, N, M
for i in range(N):
for j in range(M):
if cheezes[i][j] >= 2:
arr[i][j] = 0
def solution():
result = 0
while check_cheeze_left():
delete_cheeze(check_cheeze())
result += 1
print(result)
solution()