第二个测试点有错,90分求助

P1434 [SHOI2002] 滑雪

_Stay_ @ 2021-11-27 07:53:10

#include<bits/stdc++.h>
using namespace std;
int n,m;
int fx[5]={0,0,1,-1};
int fy[5]={1,-1,0,0};
int a[1001][1001],jy[1001][1001];
int dfs(int x,int y){
    int x1,y1;
    if(jy[x][y]!=0) return jy[x][y];
    jy[x][y]=1;
    for(int i=0;i<=4;i++)
    {
        x1=x+fx[i];
        y1=y+fy[i];
        if(x1>0 and y1>0 and x1<=n and y1<=m and a[x][y]>a[x1][y1])
        {
            dfs(x1,y1);
            jy[x][y]=max(jy[x][y],jy[x1][y1]+1);
        }
    }
    return jy[x][y];
 } 
int main()
{
    cin>>m>>n;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    cin>>a[i][j];
    int ans=0;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    ans=max(ans,dfs(i,j ));
    cout<<ans;
    return 0;
 }

by __Chtholly @ 2021-11-27 08:36:06

for(int i=0;i<=4;i++)        

改为

for(int i=0;i<4;++i)

by __Chtholly @ 2021-11-27 08:36:36

还有就是读入的时候是先读n再读m,你弄反了


by __Chtholly @ 2021-11-27 08:37:10

#include<bits/stdc++.h>
using namespace std;
int n,m;
int fx[5]={0,0,1,-1};
int fy[5]={1,-1,0,0};
int a[1001][1001],jy[1001][1001];
int dfs(int x,int y){
    int x1,y1;
    if(jy[x][y]!=0) return jy[x][y];
    jy[x][y]=1;
    for(int i=0;i<4;i++)
    {
        x1=x+fx[i];
        y1=y+fy[i];
        if(x1>0 and y1>0 and x1<=n and y1<=m and a[x][y]>a[x1][y1])
        {
            dfs(x1,y1);
            jy[x][y]=max(jy[x][y],jy[x1][y1]+1);
        }
    }
    return jy[x][y];
 } 
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    cin>>a[i][j];
    int ans=0;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    ans=max(ans,dfs(i,j ));
    cout<<ans;
    return 0;
 }

改后


by __Chtholly @ 2021-11-27 08:37:29

@Stay


by _Stay_ @ 2021-11-27 08:40:05

@Hachieve 谢谢谢谢


|