题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026

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.

 
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.
XXXXX1
5 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)
FINISH
It 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)
FINISH
God please help our poor hero.
FINISH
 
笔记:1.以前写bfs时都是直接使用的queue,其队顶元素为front(),使用优先队列一般需要写明以什么优先,即friend bool operator < (node a,node b)···且队顶元素为top()
   2.需要记录路径时,需要记录其前驱位置,以便输出。
   3.在输出时,直接使用递归输出就好,即从出口一直递归到入口,即只有一条路径!
 
 #include<iostream>
#include<queue>
#include<cstdio> using namespace std; struct node{
int x,y;
int time;
friend bool operator < (node a,node b){//优先队列!!
return a.time > b.time;
}
}; struct M{
char data;
int prex,prey;
int time;
}map[][]; int n,m;
int dir[][]={,,-,,,,,-}; bool isInMap(int x,int y){
return x>=&&x<n&&y>=&&y<m;
} int bfs(){
node cur,next;
priority_queue<node> q;
cur.x = ;
cur.y = ;
cur.time = ;
map[][].data = 'X';
q.push(cur);
while(!q.empty()){
cur = q.top();
q.pop();
if(cur.x == n- && cur.y == m-)
return cur.time;
for(int i=;i<;i++){
int xx = cur.x + dir[i][];
int yy = cur.y + dir[i][];
if(isInMap(xx,yy) && map[xx][yy].data == '.'){
next.x = xx;
next.y = yy;
next.time = cur.time+;
map[xx][yy].prex = cur.x;
map[xx][yy].prey = cur.y;
map[xx][yy].time = ;
map[xx][yy].data = 'X';
q.push(next);
}
else if(isInMap(xx,yy) && map[xx][yy].data!='X'){
next.x = xx;
next.y = yy;
next.time = cur.time + (map[xx][yy].data-'') + ;//记得加上通过时间
map[xx][yy].prex = cur.x;
map[xx][yy].prey = cur.y;
map[xx][yy].time = map[xx][yy].data-'';
map[xx][yy].data = 'X';
q.push(next);
}
}
}
return -;
} void printPath(int x,int y,int time){
if(time>){
if(map[x][y].time--){
printPath(x,y,time-);
printf("%ds:FIGHT AT (%d,%d)\n",time--,x,y);
}
else{
printPath(map[x][y].prex,map[x][y].prey,time-);
printf("%ds:(%d,%d)->(%d,%d)\n",time--,map[x][y].prex,map[x][y].prey,x,y);
}
}
return ;
} int main(){
while(cin>>n>>m){
for(int i=;i<n;i++){
getchar();
for(int j=;j<m;j++){
scanf("%c",&map[i][j].data);
}
}
int time = bfs();
if(time>=){
printf("It takes %d seconds to reach the target position, let me show you the way.\n",time);
printPath(n-,m-,time);
}
else{
printf("God please help our poor hero.\n");
}
printf("FINISH\n");
}
return ;
}

hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)的更多相关文章

  1. HDU 1026 Ignatius and the Princess I(BFS+记录路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  3. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

  4. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  6. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  8. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. 复旦大学2013--2014学年第一学期(13级)高等代数I期末考试第七大题解答

    七.(本题10分)设 \(A\) 为数域 \(K\) 上的 \(n\) 阶非异阵, 证明: 对任意的对角阵 \(B\in M_n(K)\),  \(A^{-1}BA\) 均为对角阵的充分必要条件是 \ ...

  2. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  3. crontab执行shell脚本

    */5 * * * * cd /data/**/ && ./*.sh * * * * * /bin/sh /home/*.sh

  4. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  5. 表头表侧边固定,方法二,丫的,复制td

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms

    Whenever we commit after entering data in Oracle Forms, many triggers fires during this event and al ...

  7. JSONObject.fromObject

    JSONObject.fromObjectjava.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRun ...

  8. python 列表函数

    list函数: 功能:将字符创转化为列表,例: 列表基本函数: 1.元素赋值,例: 注意:通过list[0]= 'hel',如果原来位置上有值,会覆盖掉原来的. 2.分片操作 1)显示序列,例: 注意 ...

  9. 迪米特法则(LoD),即最少知识原则

    解释: 如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用. 重点: 在类的结构上,每个类都应当尽量降低成员 ...

  10. wordpress中文标签无法访问的解决方法

    wordpress中文标签无法访问的解决方法  爱好  2年前 (2014-05-29)  7,601  8 当博客从华夏名网转移到阿里云之后,发现了不少问题,其中一个就是wordpress中文标签无 ...