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
| import sys
sys.setrecursionlimit(100000)
directions = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0]
]
AVAILABLE = -1
NOT_AVAILABLE = -2
def init(visit_map, source_map, n):
for i in range(n):
for j in range(n):
visit_map[i][j] = source_map[i][j]
def watering(visit_map, target_value, n):
global AVAILABLE,NOT_AVAILABLE
for i in range(n):
for j in range(n):
if visit_map[i][j] <= target_value:
visit_map[i][j] = NOT_AVAILABLE
else:
visit_map[i][j] = AVAILABLE
def dfs(x,y, visit_map, target_value, n):
global AVAILABLE
for i in range(4):
nx = x + directions[i][0]
ny = y + directions[i][1]
if nx < 0 or ny < 0 or nx >= n or ny >= n:
continue
if visit_map[nx][ny] != AVAILABLE:
continue
visit_map[nx][ny] = target_value
dfs(nx,ny, visit_map, target_value, n)
def find_safe_place(visit_map, n):
count = 1
for i in range(n):
for j in range(n):
if visit_map[i][j] == AVAILABLE:
visit_map[i][j] = count
dfs(i,j, visit_map, count, n)
count += 1
return count - 1
if __name__ == "__main__":
n = int(input())
source_map = []
for _ in range(n):
source_map.append(list(map(int, input().split())))
max_value = 0
for i in range(0, 100):
visit_map = [[0] * n for _ in range(n) ]
init(visit_map, source_map, n)
watering(visit_map, i, n)
max_value = max(max_value, find_safe_place(visit_map, n))
print(max_value)
|