hdu 1026 Ignatius and the Princess I【bfs+路径打印】经典题目
来源:互联网 发布:阿里先锋软件 编辑:程序博客网 时间:2024/05/14 08:44
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15326 Accepted Submission(s): 4863
Special Judge
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
虽然非常忙,但是还是忍不住敲代码............
非常庆幸遇到了这道题,本来以为再也找不到路径打印的题目练练思路了呢,结果这道题出现在我的眼前,a吧!
比着上一道做过的路径打印题目,这次的更麻烦,更复杂,首先需要用优先队列找出最短路径,然后是递归打印,而且递归打印路径时还要特殊考虑在这一点是否发生过战斗......
第三次才AC,第一次没注意范围,re 了,然后第二次发现,自己路径打印中的时间统计有问题,额..改掉就AC了.....
继续努力吧...算了,还是先好好复习吧....
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;struct migong{int x,y,time;int site,pre;//记录标号 bool friend operator <(migong a,migong b){return a.time>b.time;}}mz[10005];int n,m,vis[105][105];int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};char map[105][105];void print(int i){if(mz[i].pre!=1){print(mz[i].pre);}if(map[mz[i].x][mz[i].y]!='.'){int tp=map[mz[i].x][mz[i].y]-'0';printf("%ds:(%d,%d)->(%d,%d)\n",mz[i].time-tp,mz[mz[i].pre].x,mz[mz[i].pre].y,mz[i].x,mz[i].y);for(int j=0;j<tp;++j){printf("%ds:FIGHT AT (%d,%d)\n",mz[i].time-tp+j+1,mz[i].x,mz[i].y);//战斗的时间注意考虑 }}else{printf("%ds:(%d,%d)->(%d,%d)\n",mz[i].time,mz[mz[i].pre].x,mz[mz[i].pre].y,mz[i].x,mz[i].y);}}void bfs(int bx,int by,int ex,int ey){memset(vis,0,sizeof(vis));priority_queue<migong> q;migong st={bx,by,0,1,0};mz[1]=st;q.push(st);vis[bx][by]=1;int cnt=1;while(!q.empty()){st=q.top();q.pop();if(st.x==ex&&st.y==ey){printf("It takes %d seconds to reach the target position, let me show you the way.\n",st.time);print(st.site);return ;}for(int i=0;i<4;++i){int tx=st.x+dx[i],ty=st.y+dy[i];if(tx<0||tx>n-1||ty<0||ty>m-1||map[tx][ty]=='X'||vis[tx][ty]){continue;}int tt=st.time+1;if(map[tx][ty]!='.'){tt+=map[tx][ty]-'0';}migong tp={tx,ty,tt,++cnt,st.site};mz[cnt]=tp;q.push(tp);vis[tx][ty]=1;}}printf("God please help our poor hero.\n");}int main(){//freopen("shuju.txt","r",stdin);while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;++i){scanf("%s",&map[i]);}bfs(0,0,n-1,m-1);printf("FINISH\n");}return 0;}
0 0
- hdu 1026 Ignatius and the Princess I【bfs+路径打印】经典题目
- HDU 1026 Ignatius and the Princess I BFS打印路径
- hdoj 1026 Ignatius and the Princess I 【BFS + 优先队列 + stack路径记录】 【经典题目】
- HDU 1026 Ignatius and the Princess I && BFS+记录路径
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
- HDU--1026:Ignatius and the Princess I (BFS+优先队列+打印路径)
- HDU ACM 1026 Ignatius and the Princess I -> BFS+优先队列+路径打印
- HDU 1026 Ignatius and the Princess I(优先队列+BFS+打印路径)
- hdoj 1026 Ignatius and the Princess I【bfs&&路径输出】
- Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
- hdu 1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I(bfs+priority_queue)
- HDU-1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I BFS+剪枝
- hdu 1026 Ignatius and the Princess I bfs+优先级队列
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I----bfs+递归
- Hdu 1026 bfs Ignatius and the Princess I
- Linux 中如何查询和卸载已安装的软件
- MLlib回归算法(线性回归、决策树)实战演练--Spark学习(机器学习)
- 二叉树的操作
- 【郑轻】[1765]Lay a pancake!!!
- STL迭代器失效问题
- hdu 1026 Ignatius and the Princess I【bfs+路径打印】经典题目
- uva 11387——The 3-Regular Graph
- SPI (Service Provider Interface)
- js的一些学习笔记2
- idea15激活教程
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中判断不等于end())、operator[])
- avalon 学习资料
- 地图sd文件发布教程 Arcgis
- 简单的C程序<一>:字符间空格处理