50分求助,全MLE了

P1434 [SHOI2002] 滑雪

guang_zi_guei_ji @ 2024-08-05 09:21:47

#include<iostream>
using namespace std;
int a[110][110],b[110][110],n,m,Max=0;
bool jump(int sum,int x,int y){
    if(x>n||x<1||y>m||y<1||a[x][y]>sum){
        return 0;
    }
    return 1;
}
int dfs(int x,int y){
    if(b[x][y]>0){
        return b[x][y];
    }
    bool i=jump(a[x][y],x+1,y),j=jump(a[x][y],x-1,y),k=jump(a[x][y],x,y+1),l=jump(a[x][y],x,y-1);
    int Max1=0;
    if(i&&Max1<dfs(x+1,y)){
        Max1=dfs(x+1,y)+1;
    }
    if(j&&Max1<dfs(x-1,y)){
        Max1=dfs(x-1,y)+1;
    }
    if(k&&Max1<dfs(x,y+1)){
        Max1=dfs(x,y+1)+1;
    }
    if(l&&Max1<dfs(x,y-1)){
        Max1=dfs(x,y-1)+1;
    }
    if(!i&&!j&&!k&&!l){
        return 1;
    }
    b[x][y]=Max1;
    return Max1;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            b[i][j]=dfs(i,j);
            if(b[i][j]>Max){
                Max=b[i][j];
            }
        }
    }
    cout<<Max;
    return 0;
}

by xuzihan1008 @ 2024-08-08 20:51:21

@guang_zi_guei_ji加记忆化


|