-
Notifications
You must be signed in to change notification settings - Fork 0
/
210411_BOJ_2468.py
47 lines (34 loc) · 1.03 KB
/
210411_BOJ_2468.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
from collections import deque
import sys
input = sys.stdin.readline
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
def bfs(x, y, height):
queue = deque()
queue.append((x, y))
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and graph[nx][ny] > height:
visited[nx][ny] = True
queue.append((nx, ny))
n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]
max_height = 0
for row in graph:
max_height = max(max_height, max(row))
result = 1
for height in range(max_height):
# for height in range(max(map(max, graph))):
visited = [[False] * n for _ in range(n)]
safe_zone = 0
for i in range(n):
for j in range(n):
if graph[i][j] > height and not visited[i][j]:
safe_zone += 1
visited[i][j] = True
bfs(i, j, height)
result = max(result, safe_zone)
print(result)