记忆化搜索样例少了1求助

P1434 [SHOI2002] 滑雪

i_am_a_joker @ 2022-09-03 21:26:21

RT,输出24

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int s[105][105],f[105][105];
bool vis[105][105];
int dx[] = {0,1,-1,0};
int dy[] = {1,0,0,-1};
int dfs(int x,int y)
{
    int &now = f[x][y];//相当于将now和f[x][y]绑到了一起,两个值任意一个发生变化另一个也会变化
    if(vis[x][y]) return now;
    vis[x][y] = 1;
    for(int i=0; i<4; i++)
    {
        int tx = x+dx[i],ty = y+dy[i];
        if(tx<1 || ty<1 || tx>n || ty>m) continue;
        if(s[x][y] > s[tx][ty])
        {
            now = max(now,dfs(tx,ty)+1);
        }
    }
    return now;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            scanf("%d",&s[i][j]);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            ans = max(ans,dfs(i,j));
        }
    printf("%d",ans);
    return 0;
}

by isbfpoiaewfohqaw @ 2022-09-10 00:02:39

将f数组全部初始化为1就好了


|