蒟蒻求助!DFS未开记忆化,1个TLE+4个WA

P1434 [SHOI2002] 滑雪

CHIMPANCAKE @ 2022-07-21 16:29:05

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
long long map[105][105];
bool vis[105][105];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
long long n,m,ans=-1;
long long max(long long a,long long b){return a>b?a:b;}
void dfs(long long x,long long y,long long sum)
{
    ans=max(ans,sum);
    if(x<1||y<1||x>n||y>m) return ;
    for(int i=0;i<4;i++)
        if((x+dx[i])>0&&(y+dy[i])>0&&x+dx[i]<=n&&y+dy[i]<=m&&map[x][y]<map[x+dx[i]][y+dy[i]])
            dfs(x+dx[i],y+dy[i],sum+1);
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%lld",&map[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            dfs(1,1,1);
    printf("%lld",ans);
    return 0;
}

by 13802919466djh @ 2022-07-21 16:36:27

头像应急食品好评


by osky123456 @ 2022-07-21 16:46:41

必须开记忆化


|