Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 673 Bytes

1937.md

File metadata and controls

39 lines (29 loc) · 673 Bytes

1937

Python

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

dr = (0, 1, 0, -1)
dc = (1, 0, -1, 0)

def dfs(r, c):
    if dp[r][c]:
        return dp[r][c]

    dp[r][c] = 1
    for d in range(4):
        nr = r + dr[d]
        nc = c + dc[d]
        if nr < 0 or nc < 0 or nr >= N or nc >= N:
            continue
        if lst[r][c] < lst[nr][nc]:
            dp[r][c] = max(dp[r][c], dfs(nr, nc) + 1)

    return dp[r][c]

N = int(input())
lst = [list(map(int, input().split())) for _ in range(N)]

dp = [[0] * N for _ in range(N)]
ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans, dfs(i, j))

print(ans)