标签:des style blog http color os io strong ar
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 77134 | Accepted: 28672 |
Description
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Input
Output
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
Source
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=100+5; 8 const int INF=0x3f3f3f3f; 9 int a[MAXN][MAXN]; 10 int vis[MAXN][MAXN]; 11 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; 12 int n,m,ans,maxn; 13 void DFS(int x,int y,int cur) 14 { 15 int xx,yy; 16 if(cur>maxn) 17 maxn=cur; 18 if(a[x][y]<=a[x][y+1] && a[x][y]<=a[x+1][y] && a[x][y]<=a[x][y-1] && a[x][y]<=a[x-1][y]) 19 return ; 20 for(int k=0;k<4;k++) 21 { 22 xx=x+dir[k][0]; 23 yy=y+dir[k][1]; 24 if(a[xx][yy]<a[x][y] && 0<=xx && xx<n && 0<=yy && yy<m) 25 { 26 vis[xx][yy]=1; 27 DFS(xx,yy,cur+1); 28 vis[xx][yy]=0; 29 } 30 } 31 } 32 int main() 33 { 34 //freopen("in.txt","r",stdin); 35 while(scanf("%d %d",&n,&m)!=EOF) 36 { 37 for(int i=0;i<n;i++) 38 for(int j=0;j<m;j++) 39 scanf("%d",&a[i][j]); 40 41 for(int i=0;i<n;i++) 42 { 43 for(int j=0;j<m;j++) 44 { 45 memset(vis,0,sizeof(vis)); 46 DFS(i,j,1); 47 } 48 } 49 cout<<maxn<<endl; 50 } 51 return 0; 52 }
记忆化处理后AC代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=100+5; 8 const int INF=0x3f3f3f3f; 9 int a[MAXN][MAXN],vis[MAXN][MAXN],dp[MAXN][MAXN]; 10 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; 11 int n,m,ans,maxn,len; 12 int solve(int x,int y) 13 { 14 if(dp[x][y]!=0) 15 return dp[x][y]; 16 maxn=0; 17 for(int k=0;k<4;k++) 18 { 19 int xx=x+dir[k][0]; 20 int yy=y+dir[k][1]; 21 if(0<=xx&&xx<n && 0<=yy&&yy<m && a[x][y]>a[xx][yy]) 22 { 23 len=solve(xx,yy); 24 maxn=max(maxn,len); 25 } 26 } 27 dp[x][y]=maxn+1; 28 return maxn+1; 29 } 30 int main() 31 { 32 //freopen("in.txt","r",stdin); 33 while(scanf("%d %d",&n,&m)!=EOF) 34 { 35 memset(dp,0,sizeof(dp)); 36 ans=-1; 37 for(int i=0;i<n;i++) 38 for(int j=0;j<m;j++) 39 scanf("%d",&a[i][j]); 40 41 for(int i=0;i<n;i++) 42 { 43 for(int j=0;j<m;j++) 44 { 45 dp[i][j]=solve(i,j); 46 if(dp[i][j]>ans) 47 ans=dp[i][j]; 48 } 49 } 50 cout<<ans<<endl; 51 } 52 return 0; 53 }
标签:des style blog http color os io strong ar
原文地址:http://www.cnblogs.com/clliff/p/3952387.html