-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathac19p5.py
30 lines (26 loc) · 799 Bytes
/
ac19p5.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
import sys
sys.setrecursionlimit(150001)
input = sys.stdin.readline
def travel(cX, cY, len=0):
global maximum
length = 0
if memo[cX][cY] != 0:
return memo[cX][cY] + len
for i, j in moves:
if 0 <= cX + i < dim and 0 <= cY + j < dim and grid[cX + i][cY + j] > grid[cX][cY]:
length = max(length, travel(cX + i, cY + j, len + 1) - len)
memo[cX][cY] = length
if length > maximum:
maximum = length
return length + len
dim = int(input())
grid = [[int(x) for x in input().split()] for i in range(dim)]
# print(grid)
smallT = [0] * dim
memo = [smallT.copy() for _ in range(dim)]
moves = [[1, 0], [0, 1], [-1, 0], [0, -1]]
maximum = 0
for x in range(dim - 1, -1, -1):
for y in range(dim - 1, -1, -1):
travel(x, y)
print(maximum)