1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n,m,g[N][N]; int f[N][N]; int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0};
int dp(int i,int j){ if(f[i][j] != -1) return f[i][j]; f[i][j] = 1; for(int a=0;a<4;a++){ int x = i + dx[a]; int y = j + dy[a]; if(x>=1 && x<=n && y>=1 && y<= m && g[i][j] > g[x][y]){ f[i][j] = max(f[i][j],dp(x,y)+1); } } return f[i][j]; }
int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> g[i][j]; } } memset(f,-1,sizeof(f)); int res = 1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ res = max(res,dp(i,j)); } } cout << res; return 0; }
|