Skip to content

Commit a95851a

Browse files
committed
[23.01.29/Python] 안전 영역
1 parent 49ecd40 commit a95851a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Python_BOJ_2023/2468.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 안전 영역
2+
from collections import deque
3+
import sys
4+
input = lambda: sys.stdin.readline()
5+
n = int(input())
6+
graph = [list(map(int, input().split())) for _ in range(n)]
7+
max_height = max(max(graph))
8+
9+
answer = list()
10+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
11+
12+
def bfs(x, y):
13+
queue = deque()
14+
queue.append((x, y))
15+
visited[x][y] = True
16+
while queue:
17+
xx, yy = queue.popleft()
18+
for i in range(4):
19+
nx, ny = xx + dx[i], yy + dy[i]
20+
if 0 <= nx < n and 0 <= ny < n:
21+
if not visited[nx][ny] and graph[nx][ny] > 0:
22+
queue.append((nx, ny))
23+
visited[nx][ny] = True
24+
25+
def decreasing_height():
26+
for i in range(n):
27+
for j in range(n):
28+
if graph[i][j] > 0:
29+
graph[i][j] -= 1
30+
31+
while max_height > 0:
32+
visited = [[False] * n for _ in range(n)]
33+
cnt_building = 0
34+
for i in range(n):
35+
for j in range(n):
36+
if not visited[i][j] and graph[i][j] > 0:
37+
bfs(i, j)
38+
cnt_building += 1
39+
40+
answer.append(cnt_building)
41+
max_height -= 1
42+
decreasing_height()
43+
44+
print(max(answer))

0 commit comments

Comments
 (0)