萌新求助 #2 TLE 如何优化

P1434 [SHOI2002] 滑雪

tidongCrazy @ 2019-08-10 13:47:01

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool visit[101][101]={};
int n,m,ans=0,a[101][101],maxn=0;
void read(int &a){
    a=0;int d=1;char c;
    while(c=getchar(),c<'0'||c>'9')if(c=='-')d=-1;a=a*10+c-48;
    while(c=getchar(),c>='0'&&c<='9')a=a*10+c-48;
    a*=d;
}
void write(int x){
    if(x<0){x=-x;putchar(45);}
    if(x)write(x/10);
    else return;
    putchar(x%10+48);
}
void dfs(int x,int y,int sum){
    int fx[4]={1,0,-1,0},fy[4]={0,1,0,-1};
    for(int i=0;i<4;i++){
        int nx=x+fx[i],ny=y+fy[i];
        if(nx<1||nx>n||ny<1||ny>m||a[nx][ny]>=a[x][y]||visit[nx][ny])continue;
        visit[nx][ny]=true;
        dfs(nx,ny,sum+1);
        maxn=max(maxn,sum+1);
        visit[nx][ny]=false;
    }
}
int main(){
    read(n);read(m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            read(a[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            visit[i][j]=true;
            dfs(i,j,0);
            visit[i][j]=false;
        }
    }
    write(maxn+1);
}

by X_WT @ 2019-08-10 13:52:00

@提动Crazy 记忆化……


by tidongCrazy @ 2019-08-10 13:52:30

我不会记忆化####


by X_WT @ 2019-08-10 13:56:31

@提动Crazy 看题解


by tidongCrazy @ 2019-08-10 14:00:38

@[X_WT]() ###


by tidongCrazy @ 2019-08-10 14:06:04

@X_WT


by tidongCrazy @ 2019-08-10 14:06:16

谢谢


|