90分求助!

P1434 [SHOI2002] 滑雪

kbl_sxm1696DEshen @ 2024-08-29 09:39:08

为什么!90分!就差一个测试点WA...

#include<bits/stdc++.h>
using namespace std;
//滑雪
const int N=101;
int m[N][N],f[N][N];
int dx[5]={0,-1,0,1,0},dy[5]={0,0,1,0,-1};
int r,c,t,tmp,ans,h,nx,ny;
int search(int x,int y){
    if(f[x][y]>0) return f[x][y];
    h=1;
    for(int i=1;i<=4;i++){
        nx=x+dx[i];
        ny=y+dy[i];
        if(nx>=1&&nx<=r&&ny>=1&&ny<=c&&m[x][y]<m[nx][ny]){
            tmp=search(nx,ny)+1;
            if(tmp>h) h=tmp;
        }
    }
    f[x][y]=h;
    return h;
}
int main(){
    cin>>r>>c;
    ans=INT_MIN;
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            cin>>m[i][j];
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++){
            t=search(i,j);
            f[i][j]=t;
            if(t>ans) ans=t;
        }
    cout<<ans<<endl;
    return 0;
}

还有很多要优化的地方但是为什么通不过呢?求助...


by Yxy7952 @ 2024-08-29 10:07:37

@kbl_sxm1696DEshen @kbl_sxm1696DEshen

#include<bits/stdc++.h>
using namespace std;
//滑雪
const int N=101;
int m[N][N],f[N][N];
int dx[5]={0,-1,0,1,0},dy[5]={0,0,1,0,-1};
int r,c,t,tmp,ans,h,nx,ny;
int search(int x,int y){
    if(f[x][y]>0) return f[x][y];
    h=1;
    for(int i=1;i<=4;i++){
        nx=x+dx[i];
        ny=y+dy[i];
        if(nx>=1&&nx<=r&&ny>=1&&ny<=c&&m[x][y]>m[nx][ny]){//小于改成大于,是从x,y走到nx,ny
            tmp=search(nx,ny)+1;
            if(tmp>h) h=tmp;
        }
    }
    f[x][y]=h;
    return h;
}
int main(){
    cin>>r>>c;
    ans=INT_MIN;
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            cin>>m[i][j];
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++){
            t=search(i,j);
            f[i][j]=t;
            if(t>ans) ans=t;
        }
    cout<<ans<<endl;
    return 0;
}

by kbl_sxm1696DEshen @ 2024-08-29 10:23:40

@yixingyou 哪里有BUG?帮我找找BUG吧,90分真的好难受!


by Yxy7952 @ 2024-08-29 10:30:20

@kbl_sxm1696DEshen

不是注释了吗???


by xuyunlong120820 @ 2024-08-29 10:33:02

虽然这不好,但是 骗分(看报错提示) 输出改为

if(ans==4){
    cout<<4<<"\n";
    return 0;
}
cout<<ans<<"\n";

by kbl_sxm1696DEshen @ 2024-08-29 11:45:32

@yixingyou 芜湖谢谢【刚刚没瞅见


|