TLE#2

P1434 [SHOI2002] 滑雪

laybare_mushroom @ 2024-08-25 18:13:01

#include<bits/stdc++.h>
using namespace std;
int R,C,dp[300][300],vis[101][101],lu=1,maxn=-1,sum=0;
void dfs(int xa,int ya){
    if(xa+1<=R){
        if(vis[xa+1][ya]==0&&dp[xa+1][ya]<dp[xa][ya]){
            lu++;
            //cout<<xa<<" "<<ya<<" ";
        vis[xa+1][ya]=-1;
        dfs(xa+1,ya);
        }

    }
    if(ya+1<=C){
        if(vis[xa][ya+1]==0&&dp[xa][ya+1]<dp[xa][ya]){
            lu++;
        //  cout<<xa<<" "<<ya<<" ";
        vis[xa][ya+1]=-1;
        dfs(xa,ya+1);
        }

    }
    if(xa-1>=1){
        if(vis[xa-1][ya]==0&&dp[xa-1][ya]<dp[xa][ya]){
        lu++;
        //cout<<xa<<" "<<ya<<" ";
        vis[xa-1][ya]=-1;
        dfs(xa-1,ya);   
        }

    }
    if(ya-1>=1){
        if(vis[xa][ya-1]==0&&dp[xa][ya-1]<dp[xa][ya]){
            lu++;
            //cout<<xa<<" "<<ya<<" ";
        vis[xa][ya-1]=-1;
        dfs(xa,ya-1);
        }

    }
    if(lu>maxn){
        maxn=lu;
    }
    lu--;
    vis[xa][ya]=0;
    //return;
}
int main(){
    cin>>R>>C;
    memset(dp,0,sizeof(dp));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=R;i++){
        for(int j=1;j<=C;j++){
            scanf("%d",&dp[i][j]);
        }
    }
    for(int i=1;i<=R;i++){
        for(int j=1;j<=C;j++){
            if(i+1<=R){
                if(dp[i+1][j]>dp[i][j]){
                    continue;
                }
            }
            if(i-1>=1){
                if(dp[i-1][j]>dp[i][j]){
                    continue;
                }
            }
            if(j+1<=C){
                if(dp[i][j+1]>dp[i][j]){
                    continue;
                }
            }
            if(j-1>=1){
                if(dp[i][j-1]>dp[i][j]){
                    continue;
                }
            }
            vis[i][j]=-1;
            //cout<<i<<j;
            dfs(i,j);
            lu=1;
            vis[i][j]=0;
        }
    }
    cout<<maxn;
    return 0;
}

|