POJ 1088滑雪 (记忆化DP)题解
2014-11-15 09:14
465 查看
//用记忆化搜索 /*** * *在深度优先搜索的过程中把已经搜索过的点的步数保存下来,这样可以避免点的重复搜索 *提高了搜索的效率。其实质相当于从上到下的动态规划。 ***/ #include <stdio.h> #include <algorithm> #define MAX 123456 using namespace std; //记忆化搜索+深度优先搜索实质是dp int R,C,map[105][105],step[105][105]; int xx[]={-1,0,1,0}; int yy[]={0,1,0,-1}; int dfs(int x,int y){ if(step[x][y]!=0){//由于step[x][y]中保存的已经是当前点的最大步数,不用再递归搜索,只需要返回该值即可 return step[x][y]; } int ans=1; for(int i=0;i<4;i++){//上下左右四个方位的点的遍历 int tmpx=x+xx[i]; int tmpy=y+yy[i]; if(tmpx>=0&&tmpx<R&&tmpy>=0&&tmpy<C){//判断是否越界 if(map[x][y]>map[tmpx][tmpy]){//判断当前点的高度是否比周围的点高 int tmp=dfs(tmpx,tmpy);//深度优先搜索,找出周围点的最大步数 if(tmp>=ans){ ans=tmp+1; } } } } step[x][y]=ans;//保存当前点的最大步数 return step[x][y]; } int main(){ while(~scanf("%d %d",&R,&C)){ for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ scanf("%d",&map[x][y]); step[x][y]=0; } } int ans=0,tmp=0; for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ tmp=dfs(x,y); if(ans<tmp) ans=tmp; } } printf("%d\n",ans); } return 0; }
//用记忆化搜索 /*** * *在深度优先搜索的过程中把已经搜索过的点的步数保存下来,这样可以避免点的重复搜索 *提高了搜索的效率。其实质相当于从上到下的动态规划。 ***/ #include <stdio.h> #include <algorithm> #define MAX 123456 using namespace std; //记忆化搜索+深度优先搜索实质是dp int R,C,map[105][105],step[105][105]; int xx[]={-1,0,1,0}; int yy[]={0,1,0,-1}; int dfs(int x,int y){ if(step[x][y]!=0){//由于step[x][y]中保存的已经是当前点的最大步数,不用再递归搜索,只需要返回该值即可 return step[x][y]; } int ans=1; for(int i=0;i<4;i++){//上下左右四个方位的点的遍历 int tmpx=x+xx[i]; int tmpy=y+yy[i]; if(tmpx>=0&&tmpx<R&&tmpy>=0&&tmpy<C){//判断是否越界 if(map[x][y]>map[tmpx][tmpy]){//判断当前点的高度是否比周围的点高 int tmp=dfs(tmpx,tmpy);//深度优先搜索,找出周围点的最大步数 if(tmp>=ans){ ans=tmp+1; } } } } step[x][y]=ans;//保存当前点的最大步数 return step[x][y]; } int main(){ while(~scanf("%d %d",&R,&C)){ for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ scanf("%d",&map[x][y]); step[x][y]=0; } } int ans=0,tmp=0; for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ tmp=dfs(x,y); if(ans<tmp) ans=tmp; } } printf("%d\n",ans); } return 0; }
/*** *该方法是通过从每个点出发宽度优先搜索,记录下从当前点出发 *可以到达的最远的步数 *该算法的缺点是:相对于前两个种方法,时间复杂度很高,在快搜的过程中 *有很多点被重复的搜索过。 ***/ #include<stdio.h> #include<string.h> #include<queue> #define MAX 123456 using namespace std; typedef struct node{ int x; int y; int w; }Node; int R,C,map[105][105],step[105][105]//记录的是到当前点的最大步数; int maxx; int xx[]={-1,0,1,0}; int yy[]={0,1,0,-1}; void deal(Node p){ maxx=0; queue<Node>Que; Que.push(p); Node t; while(!Que.empty()){ p=Que.front(); Que.pop(); for(int i=0;i<4;i++){ t.x=p.x+xx[i]; t.y=p.y+yy[i]; t.w=map[t.x][t.y]; if(t.x>=0&&t.x<R&&t.y>=0&&t.y<C&&map[p.x][p.y]>t.w){//限界条件 if(step[p.x][p.y]>=step[t.x][t.y]){//剪枝条件 step[t.x][t.y]=step[p.x][p.y]+1; if(maxx<step[t.x][t.y]) maxx=step[t.x][t.y];//记录最大步数 Que.push(t); } } } } } int main(){ while(~scanf("%d %d",&R,&C)){ for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ scanf("%d",&map[x][y]); step[x][y]=-1; } } int ans=0; for(int x=0;x<R;x++){ for(int y=0;y<C;y++){ memset(step,-1,sizeof(step)); step[x][y]=1; Node p; p.x=x; p.y=y; p.w=map[x][y]; deal(p); if(ans<maxx) ans=maxx; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 1088 滑雪 记忆化DP
- POJ - 1088 滑雪(在这里dp和记忆化搜索有区别吗)
- POJ 1088 滑雪(简单的记忆化dp)
- poj1088——滑雪(记忆化dp)
- poj 1088 滑雪 (记忆化 Dp)
- POJ-1088 滑雪 (记忆化搜索,dp)
- POJ 1088 滑雪(记忆化dfs)
- POJ 1088 滑雪 记忆化搜索
- POJ1088 滑雪 dp
- POJ 1088 滑雪(DP)
- POJ-1088 滑雪 (记忆化搜索,dp)
- Poj_1088_滑雪(DP)
- POJ 1088 滑雪 (DP)
- POJ 1088 滑雪 DP
- poj 1088 滑雪 深搜(谁说是DP了!)
- POJ 1088 滑雪 (DFS+DP)
- Poj_1088_滑雪(DP)
- POJ 1088 滑雪 (DP)
- poj 1088 经典dp 滑雪
- POJ-1088 滑雪 (dfs + 线性dp)