-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2573.py
More file actions
68 lines (53 loc) · 1.53 KB
/
2573.py
File metadata and controls
68 lines (53 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 빙산
# - pypy3 로 맞음
import sys
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
def dfs(x, y):
for direction in range(4):
nx = dx[direction]+x
ny = dy[direction]+y
if 0 <= nx < N and 0 <= ny < M and visited[nx][ny]:
visited[nx][ny] = False
if iceMap[nx][ny] != 0:
dfs(nx, ny)
N, M = map(int, input().split())
iceMap = []
for _ in range(N):
iceMap.append(list(map(int, input().split()))[:M])
year = 0
visited = [[False]*M for _ in range(N)]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
while True:
year += 1
# 빙산 녹는 처리
for i in range(N):
for j in range(M):
if iceMap[i][j] != 0:
visited[i][j] = True
state = iceMap[i][j]
for k in range(4):
nx = dx[k]+i
ny = dy[k]+j
if 0 <= nx < N and 0 <= ny < M and not visited[nx][ny]:
if iceMap[nx][ny] == 0:
state -= 1
if state == 0:
break
iceMap[i][j] = state
# 빙산 영역 체크
field = 0
for i in range(N):
for j in range(M):
if iceMap[i][j] != 0 and visited[i][j]:
dfs(i, j)
field += 1
elif iceMap[i][j] == 0 and visited[i][j]:
visited[i][j] = False
if field >= 2:
print(year)
break
elif field == 0:
print(0)
break