求助 10pts 4#6#AC

P1434 [SHOI2002] 滑雪

ytr2010 @ 2023-09-02 15:16:59

#include<bits/stdc++.h>
using namespace std;
int mp[105][105],vis[101][101],n,m,ans=0;
int dx[]={-1,1,0,0}
   ,dy[]={0,0,-1,1};
int dfs(int x,int y,int maxfrom)
{
    int sum=0;
    if(mp[x][y]>=maxfrom) return 0;
    if(mp[x][y]==1) return 1; 
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>n||nx<=0||ny>m||ny<=0) continue;
        if(vis[nx][ny]==0)
        {
            vis[nx][ny]=1;
            sum=max(sum,dfs(nx,ny,mp[x][y]));
            vis[nx][ny]=0;
        }
    }
    return sum+1;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>mp[n][m];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            vis[i][j]=1;                       
            ans=max(ans,dfs(j,i,0x7f));
            cout<<ans<<endl;
            vis[i][j]=0;  
        } 
    cout<<ans;
}

by 2011Andy @ 2023-09-02 15:22:17

#include<bits/stdc++.h>
using namespace std;
int mp[105][105],vis[101][101],n,m,ans=0;
int dx[]={-1,1,0,0}, dy[]={0,0,-1,1};
int dfs(int x,int y,int maxfrom)
{
    int sum=0;
    if(mp[x][y]>=maxfrom) return 0;
    if(mp[x][y]==1) return 1; 
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>n||nx<=0||ny>m||ny<=0) continue;
        if(vis[nx][ny]==0)
        {
            vis[nx][ny]=1;
            sum=max(sum,dfs(nx,ny,mp[x][y]));
            vis[nx][ny]=0;
        }
    }
    return sum+1;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>mp[i][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            vis[i][j]=1;                       
            ans=max(ans,dfs(i,j,0x7f));
            vis[i][j]=0;  
        } 
    cout<<ans;
    return 0;
}

输入有问题改后70pts


by lraM416 @ 2023-09-02 15:31:09

@ytr2010 你没记忆化啊(


|