https://www.luogu.org/problemnew/show/P1434

题解:DFS

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
typedef __int128 lll;
const int N=120;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,ans,ans0;
int a[N][N];
int b[][2]={1,0,0,1,-1,0,0,-1};
int c[N][N];
bool vis[N][N];
void dfs(int x,int y,int cnt){
    if(c[x][y]){
        ans=max(ans,cnt+c[x][y]-1);
        return;
    }
    ans=max(ans,cnt);
    for(RI i=0;i<4;i++){
        int tx=x+b[i][0];
        int ty=y+b[i][1];
        if(1<=tx&&tx<=n&&1<=ty&&ty<=m&&!vis[tx][ty]&&a[tx][ty]<a[x][y]){
            vis[tx][ty]=1;
            dfs(tx,ty,cnt+1);
            vis[tx][ty]=0;
        }
    }
    return;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            //memset(vis,0,sizeof(vis));
            vis[i][j]=1;
            ans=0;
            dfs(i,j,1);
            c[i][j]=ans;
            vis[i][j]=0;
            ans0=max(ans,ans0);
        }
    }
    cout << ans0 << endl;
    //cout << "Hello world!" << endl;
    return 0;
}