#2超时,#8错,不知道有没有记忆化搜索,大佬快来帮帮

P1434 [SHOI2002] 滑雪

victor916 @ 2023-10-21 22:37:17

#include <stdio.h>
int a[105][105];
int mark[105][105];
int ans,n,m;
int starti,startj;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
void f(int x,int y,int step)
{
    int flag=0;
    for(int i=0;i<4;i++)
    {
        if(x+dx[i]>n||y+dy[i]>m||x+dx[i]<1||y+dy[i]<1) continue;

        if(a[x+dx[i]][y+dy[i]]>a[x][y])
        {
            step++;
            f(x+dx[i],y+dy[i],step);
            step--;
        }
    }
    if(mark[x][y]>=step)return;
    if(step>ans)ans=step;
    mark[x][y]=step;
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            if(mark[i][j]==0)
            {
                starti=i;
                startj=j;
                f(i,j,1);
            }
        }
    }
    printf("%d",ans);
    return 0;
}

|