题目: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. loadrunner安装汉化破解

    1.解压loadrunner11,点击setup.exe安装loadrunner11. 遇到问题,"...输入命令\来解决". 解决办法:运行解压包的loadrunner-11安装 ...

  2. .Net自帶Ajax和GridView

    如圖所示,在新建web窗體后的工具欄中有一個 AJAX擴展 ScriptManager 在整個網頁中有且只有一個,使用母版頁和用戶控件中尤為注意, 例如在嵌套母版頁和用戶控件時只在最外層加上Scrip ...

  3. SQL数据库约束行为---防止数据漏填

    防止有些该填的没有填:一.非空约束不能为NULL.操作:在表的设计界面中“允许NULL值”中的复选框去掉. 二.默认值.如果不给列赋值的话,会使用默认值填上.操作:在表的设计界面,选择相应的列,在下面 ...

  4. Shared Preferences 数据存储

    SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 其背后是用xml文件存放数据,文件存放在/data/data/<package name>/s ...

  5. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  6. C++ 函数后加const

    1.非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误)2.表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,    任意修改它所在的类的成员的操作都是 ...

  7. 【转载】C++内存分配

    原文:C++内存分配 内存泄露相信对C++程序员来说都不陌生.解决内存泄露的方案多种多样,大部分方案以追踪检测为主,这种方法实现起来容易,使用方便,也比较安全. 首先我们要确定这个模块的主要功能: 能 ...

  8. Quartz实用二三事

    注意:本文项目使用的Quartz版本为2.2.1 一.关于Trigger Trigger tg = newTrigger().withIdentity("tg3", "g ...

  9. python内置函数的归集

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...

  10. phpcms标签大全V9

    转自:http://blog.csdn.net/cloudday/article/details/7343448调用头部 尾部 {template "content"," ...