您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: