标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 92384 | Accepted: 34948 |
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
题目链接:POJ 1088
做的第一道记忆化搜索的题目,每一次把最好的拓展结果记录下来再传回去,挺好理解的
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<bitset> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define INF 0x3f3f3f3f #define CLR(x,y) memset(x,y,sizeof(x)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair<int,int> pii; typedef long long LL; const double PI=acos(-1.0); const int N=110; int pos[N][N],direct[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int dp[N][N]; int n,m; int dfs(int x,int y) { if(dp[x][y]) return dp[x][y]; else { int maxm=0; for (int i=0; i<4; ++i) { int xx=x+direct[i][0]; int yy=y+direct[i][1]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&pos[xx][yy]<pos[x][y]) maxm=max<int>(maxm,dfs(xx,yy)); } return dp[x][y]=maxm+1; } } int main(void) { int i,j; while (~scanf("%d%d",&n,&m)) { CLR(dp,0); for (i=0; i<n; ++i) for (j=0; j<m; ++j) scanf("%d",&pos[i][j]); for (i=0; i<n; ++i) for (j=0; j<m; ++j) dfs(i,j); int ans=1; for (i=0; i<n; ++i) for (j=0; j<m; ++j) if(dp[i][j]>ans) ans=dp[i][j]; printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Blackops/p/5875637.html