链接:点我
记忆化搜索很好写
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 typedef long long ll; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("***** "); 15 const int MAXN=1005; 16 int n,m,tt; 17 int a[MAXN][MAXN]; 18 int dp[MAXN][MAXN]; 19 int d[4][2]={1,0,0,1,-1,0,0,-1}; 20 int fun(int x,int y) 21 { 22 if(dp[x][y]>0) return dp[x][y]; 23 dp[x][y]=1; 24 for(int i=0;i<4;i++) 25 { 26 int nx=x+d[i][0]; 27 int ny=y+d[i][1]; 28 if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[x][y]>a[nx][ny]) 29 { 30 if(dp[x][y]<fun(nx,ny)+1) 31 dp[x][y]=dp[nx][ny]+1; 32 } 33 } 34 return dp[x][y]; 35 } 36 int main() 37 { 38 int i,j,k; 39 #ifndef ONLINE_JUDGE 40 freopen("1.in","r",stdin); 41 #endif 42 while(scanf("%d%d",&n,&m)!=EOF) 43 { 44 for(i=1;i<=n;i++) 45 { 46 for(j=1;j<=m;j++) 47 { 48 scanf("%d",&a[i][j]); 49 } 50 } 51 memset(dp,-1,sizeof(dp)); 52 int ans=0; 53 for(i=1;i<=n;i++) 54 for(j=1;j<=m;j++) 55 { 56 if(fun(i,j)>ans) ans=dp[i][j]; 57 } 58 printf("%d ",ans); 59 } 60 }