-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path백준 2638번 치즈.py
More file actions
49 lines (45 loc) · 1.23 KB
/
Copy path백준 2638번 치즈.py
File metadata and controls
49 lines (45 loc) · 1.23 KB
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
# 백준 2638번 치즈
import sys
from collections import deque
input = sys.stdin.readline
def melt_cheese():
global cheese
visited = [[0 for _ in range(M)] for _ in range(N)]
queue = deque()
# 초기값 탐색
for i in range(N):
flag = 0
for j in range(M):
if cheese[i][j] == 0:
flag = 1
queue.append((i,j))
visited[i][j] = 1
break
if flag == 1:
break
flag2 = 0
while queue:
x, y = queue.popleft()
for dx, dy in [(0,1), (1,0), (0,-1), (-1,0)]:
nx, ny = x+dx, y+dy
if not (0<=nx<N and 0<=ny<M): continue
if cheese[nx][ny] == 0 and not visited[nx][ny]:
visited[nx][ny] = 1
queue.append((nx,ny))
elif cheese[nx][ny] == 1:
visited[nx][ny] += 1
flag2 = 1
for i in range(N):
for j in range(M):
if visited[i][j] >= 2:
cheese[i][j] = 0
if flag2 == 0: return False
else: return True
N, M = map(int, input().split())
cheese = []
for _ in range(N):
cheese.append(list(map(int, input().split())))
sec = 0
while melt_cheese():
sec += 1
print(sec)