hdu1026——Ignatius and the Princess I
来源:互联网 发布:矩阵的平方 编辑:程序博客网 时间:2024/05/18 15:05
Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
Sample Input
5 6.XX.1...X.2.2...X....XX.XXXXX.5 6.XX.1...X.2.2...X....XX.XXXXX15 6.XX.....XX1.2...X....XX.XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.1s:(0,0)->(1,0)2s:(1,0)->(1,1)3s:(1,1)->(2,1)4s:(2,1)->(2,2)5s:(2,2)->(2,3)6s:(2,3)->(1,3)7s:(1,3)->(1,4)8s:FIGHT AT (1,4)9s:FIGHT AT (1,4)10s:(1,4)->(1,5)11s:(1,5)->(2,5)12s:(2,5)->(3,5)13s:(3,5)->(4,5)FINISHIt takes 14 seconds to reach the target position, let me show you the way.1s:(0,0)->(1,0)2s:(1,0)->(1,1)3s:(1,1)->(2,1)4s:(2,1)->(2,2)5s:(2,2)->(2,3)6s:(2,3)->(1,3)7s:(1,3)->(1,4)8s:FIGHT AT (1,4)9s:FIGHT AT (1,4)10s:(1,4)->(1,5)11s:(1,5)->(2,5)12s:(2,5)->(3,5)13s:(3,5)->(4,5)14s:FIGHT AT (4,5)FINISHGod please help our poor hero.FINISH
Author
Ignatius.L
Recommend
BFS+优先队列+打印路径
一开始用的非递归打印路径的不知道哪里错了
,后来在网上看了一份递归的方法,拿来实现了下就AC了
先贴下WA的吧
这是AC的:
BFS+优先队列+打印路径
一开始用的非递归打印路径的不知道哪里错了
,后来在网上看了一份递归的方法,拿来实现了下就AC了
先贴下WA的吧
#include<map>#include<queue>#include<vector>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;char mat[105][105];bool vis[105][105];int ans[222][2];int st[105];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int n,m;struct node{ int x,y; int step; friend bool operator < (const node a,const node b) { return a.step>b.step; }};struct point{ int x,y; int status;}pre[105][105];bool is_legal(node temp){ if(temp.x<0 || temp.x>=n || temp.y<0 || temp.y>=m || mat[temp.x][temp.y]=='X') return false; return true;}void bfs(){ priority_queue<node>qu; memset(vis,0,sizeof(vis)); while(!qu.empty()) qu.pop(); node temp1,temp2; temp1.x=0; temp1.y=0; temp1.step=0; pre[0][0].x=temp1.x; pre[0][0].y=temp1.y; pre[0][0].status=0; vis[temp1.x][temp1.y]=1; qu.push(temp1); bool flag=false; int times; while(!qu.empty()) { temp1=qu.top(); qu.pop(); for(int i=0;i<4;i++) { temp2.x=temp1.x+dir[i][0]; temp2.y=temp1.y+dir[i][1]; temp2.step=temp1.step; if(!is_legal(temp2) || vis[temp2.x][temp2.y]) continue; vis[temp2.x][temp2.y]=1; if(mat[temp2.x][temp2.y]=='.') { pre[temp2.x][temp2.y].status=0; temp2.step++; } else if(mat[temp2.x][temp2.y]>='1' && mat[temp2.x][temp2.y]<='9') { pre[temp2.x][temp2.y].status=mat[temp2.x][temp2.y]-'0'; temp2.step+=mat[temp2.x][temp2.y]-'0'+1; } pre[temp2.x][temp2.y].x=temp1.x; pre[temp2.x][temp2.y].y=temp1.y; if(temp2.x==n-1 && temp2.y==m-1) { times=temp2.step; flag=true; break; } qu.push(temp2); } if(flag) break; } if(!flag) printf("God please help our poor hero.\n"); else { point t; t.x=n-1; t.y=m-1; int cnt=0; while(1) { ans[cnt][0]=t.x; ans[cnt][1]=t.y;// printf("%d %d\n",t.x,t.y ); if(pre[t.x][t.y].status) st[cnt]=pre[t.x][t.y].status; else st[cnt]=0; cnt++; if(t.x==0 && t.y==0) break; int x=t.x; int y=t.y; t.x=pre[x][y].x; t.y=pre[x][y].y; } //for(int i=1;i<=times-1;i++) // printf("%d %d\n",ans[i][0],ans[i][1] ); printf("It takes %d seconds to reach the target position, let me show you the way.\n",times); int j=1; for(int i=cnt-1;i>=0;i--) { if(i==0) { if(st[0]==0) break;int t=st[0]; while(t--) { printf("%ds:FIGHT AT (%d,%d)\n",j,ans[0][0],ans[0][1] ); j++; } } else if(i>0 && st[i]==0) { printf("%ds:(%d,%d)->(%d,%d)\n",j,ans[i][0],ans[i][1],ans[i-1][0],ans[i-1][1]); j++; } else if(i>0 && st[i]>0) { int t=st[i]; while(t--) { printf("%ds:FIGHT AT (%d,%d)\n",j,ans[i][0],ans[i][1] ); j++; }<pre name="code" class="cpp"> printf("%ds:(%d,%d)->(%d,%d)\n",j,ans[i][0],ans[i][1],ans[i-1][0],ans[i-1][1]); j++; } } } printf("FINISH\n");}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) scanf("%s",mat[i]); bfs(); } return 0;}
这是AC的:
#include<map>#include<queue>#include<vector>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;char mat[105][105];bool vis[105][105];int ans[1000][2];int st[105];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int n,m;struct node{ int x,y; int step; friend bool operator < (const node a,const node b) { return a.step>b.step; }};struct point{ int x,y; int status;}pre[105][105];bool is_legal(node temp){ if(temp.x<0 || temp.x>=n || temp.y<0 || temp.y>=m || mat[temp.x][temp.y]=='X') return false; return true;}void output(int x,int y,int times){ if(times>0) { if(pre[x][y].status--!=0)//需战斗 { output(x,y,times-1); printf("%ds:FIGHT AT (%d,%d)\n", times, x, y); } else { output(pre[x][y].x,pre[x][y].y,times-1); printf("%ds:(%d,%d)->(%d,%d)\n", times, pre[x][y].x, pre[x][y].y, x, y); } }}void bfs(){ priority_queue<node>qu; memset(vis,0,sizeof(vis)); while(!qu.empty()) qu.pop(); node temp1,temp2; temp1.x=0; temp1.y=0; temp1.step=0; pre[0][0].x=-1; pre[0][0].y=-1; pre[0][0].status=0; vis[temp1.x][temp1.y]=1; qu.push(temp1); bool flag=false; int times; while(!qu.empty()) { temp1=qu.top(); qu.pop(); for(int i=0;i<4;i++) { temp2.x=temp1.x+dir[i][0]; temp2.y=temp1.y+dir[i][1]; temp2.step=temp1.step; if(!is_legal(temp2) || vis[temp2.x][temp2.y]) continue; vis[temp2.x][temp2.y]=1; if(mat[temp2.x][temp2.y]=='.') { pre[temp2.x][temp2.y].status=0; temp2.step++; } else if(mat[temp2.x][temp2.y]>='1' && mat[temp2.x][temp2.y]<='9') { pre[temp2.x][temp2.y].status=mat[temp2.x][temp2.y]-'0'; temp2.step+=mat[temp2.x][temp2.y]-'0'+1; } pre[temp2.x][temp2.y].x=temp1.x; pre[temp2.x][temp2.y].y=temp1.y; if(temp2.x==n-1 && temp2.y==m-1) { times=temp2.step; flag=true; break; } qu.push(temp2); } if(flag) break; } if(!flag) printf("God please help our poor hero.\n"); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",times); output(n-1,m-1,times); } printf("FINISH\n");}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) scanf("%s",mat[i]); bfs(); } return 0;}
0 0
- HDU1026—Ignatius and the Princess I
- hdu1026——Ignatius and the Princess I
- hdu1026——Ignatius and the Princess I()
- HDU1026——Ignatius and the Princess I(BFS)
- hdu1026——Ignatius and the Princess I
- HDU1026——Ignatius and the Princess I
- hdu1026 Ignatius and the Princess I
- HDU1026:Ignatius and the Princess I
- HDU1026:Ignatius and the Princess I(BFS)
- HDU1026 Ignatius and the Princess I
- hdu1026 Ignatius and the Princess I BFS
- hdu1026 Ignatius and the Princess I
- hdu1026---Ignatius and the Princess I
- hdu1026 Ignatius and the Princess I
- HDU1026 Ignatius and the Princess I
- HDU1026:Ignatius and the Princess I(BFS)
- HDU1026 Ignatius and the Princess I
- HDU1026:Ignatius and the Princess I(BFS)
- 图片剪裁
- linux磁盘占用100%的解决方法
- main(int argc,char* argv[],char *envp[] )
- 详解Oracle DELETE和TRUNCATE 的区别
- 中秋快乐&&股市飘红--- Best Time to Buy and Sell Stock III&& II&&I
- hdu1026——Ignatius and the Princess I
- goagent mark
- 16位色、24位色、32位各含有多少种颜色?
- Oracle10g新特性——记录DML错误日志(一)
- jquery插件合集之图片裁剪
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战02 主界面创建
- HDU 3879 Base Station 最大权闭合子图
- hadoop修改权限问题
- Mysql 中insert语句的性能优化