MLE求调

P1434 [SHOI2002] 滑雪

jjhjiangjunhao @ 2024-09-08 17:19:49

#include<bits/stdc++.h>
using namespace std;
int List[101][101]={{0}},weight,height,Max=0,mx,my,ans[101][101]={0},mans=0;
void dfs(int x,int y){
    if(x<0 or x>=weight or y<0 or y>=height)return;
    if(List[x][y+1]<=List[x][y] and y+1<height and ans[x][y+1]<ans[x][y]+1){
        ans[x][y+1]=ans[x][y]+1;
        dfs(x,y+1); 
    }
    if(List[x+1][y]<=List[x][y] and x+1<weight and ans[x+1][y]<ans[x][y]+1){
        ans[x+1][y]=ans[x][y]+1;
        dfs(x+1,y); 
    }
    if(List[x][y-1]<=List[x][y] and y-1>=0 and ans[x][y-1]<ans[x][y]+1){
        ans[x][y-1]=ans[x][y]+1;
        dfs(x,y-1);
    }
    if(List[x-1][y]<=List[x][y] and x-1>=0 and ans[x-1][y]<ans[x][y]+1){
        ans[x-1][y]=ans[x][y]+1;
        dfs(x-1,y); 
    }
}
int main(){
    cin>>weight>>height;
    for(int i=0;i<weight;i++)for(int j=0;j<height;j++){
        cin>>List[i][j];
        if(List[i][j]>Max){
            Max=List[i][j];
            mx=i;
            my=j;
        }
    } 
    ans[mx][my]=1;
    dfs(mx,my);
    for(int i=0;i<weight;i++){
        for(int j=0;j<height;j++){
            mans=max(mans,ans[i][j]);
        }
    }
    cout<<mans;
    return 0;
} 

by _Vistion_ @ 2024-09-08 18:04:04

#include<bits/stdc++.h>
using namespace std;
int r,c,g[110][110],dx[4]={-1,1,0,0},dy[4]={0,0,1,-1},dp[110][110],ans=0;
int dfs(int x,int y){
    if(dp[x][y]!=0) return dp[x][y];
    dp[x][y]=1;
    for(int i=0;i<4;i++){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>r||nx<1||ny>c||ny<1) continue;
        if(g[x][y]>g[nx][ny]){
            dfs(nx,ny);
            dp[x][y]=max(dp[x][y],dp[nx][ny]+1);
        }
    }
    return dp[x][y];
}
int main(){
    cin>>r>>c;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            cin>>g[i][j]; 
        }
    }
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            dfs(i,j);
            ans=max(ans,dp[i][j]);
        }
    }
    cout<<ans;
    return 0;
}

求关


by jjhjiangjunhao @ 2024-09-09 22:36:50

@YZ_zhang 没问题


|