90分,一个case RE,球球大佬解答

P1434 [SHOI2002] 滑雪

CoderK @ 2022-10-11 18:59:20

import heapq

m,n=map(int,input().split())
nums=[]
for i in range(m):
    nums.append(list(map(int,input().split())))

memo=[[0 for i in range(n)] for  j in range(m)]
global res
res=0

def dfs(x,y,s):
    global res
    memo[x][y]=s
    for a,b in [[x,y+1],[x+1,y],[x,y-1],[x-1,y]]:
        if a>=0 and a<m and b>=0 and b<n and nums[x][y]>nums[a][b] and s>memo[a][b]:
            dfs(a,b,s+1)

    res=max(res,s)

start=[]

for i in range(m):
    for j in range(n):
        heapq.heappush(start,[-nums[i][j],i,j])

while start:
    state=heapq.heappop(start)
    num,x,y=state[0],state[1],state[2]
    if not memo[x][y]:dfs(x,y,1)

print(res)

|