Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14577    Accepted Submission(s): 4613
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.

 
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
 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1072 1175 1010 1180 1016 
这题其实可以倒着搜,就不用栈了.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 210
#define inf 0x3f3f3f3f3f3f
char Map[maxn][maxn];
int visit[maxn][maxn];
int dir[][]={,-,,,-,,,};
int n,m;
int pre_x[maxn][maxn],pre_y[maxn][maxn];
int rear_x[maxn][maxn],rear_y[maxn][maxn];
int cnt;
struct node
{
int x,y,dist=;
};
struct cmp
{
bool operator () (node a,node b)
{
if(a.dist>b.dist) //从小到大排序
return true;
else
return false;
}
}; priority_queue <node,vector <node>,cmp> pq; void init()
{
cnt=;
memset(visit,,sizeof(visit));
memset(pre_x,-,sizeof(pre_x));
memset(pre_y,-,sizeof(pre_y));
memset(rear_x,-,sizeof(rear_x));
memset(rear_y,-,sizeof(rear_y));
pre_x[][]=;
pre_y[][]=;
rear_x[n-][m-]=n-;
rear_y[n-][m-]=m-;
// for(int i=1;i<maxn;i++)
// printf("%d",pre_x[i]);
// printf("%d %d\n\n",pre_x[1],pre_y[1]);
while(!pq.empty())
pq.pop();
}
int bfs(node start)
{
pq.push(start); //值传递
node cur;
visit[start.x][start.y]=;
while(!pq.empty())
{
cur=pq.top();
pq.pop();
if(cur.x==n- && cur.y==m-)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",cur.dist);
int father_x=cur.x,father_y=cur.y,xx,yy;
while(pre_x[father_x][father_y]!=father_x || pre_y[father_x][father_y]!=father_y)
{
xx=father_x; yy=father_y;
father_x=pre_x[xx][yy];
father_y=pre_y[xx][yy];
rear_x[father_x][father_y]=xx;
rear_y[father_x][father_y]=yy;
}
int son_x=,son_y=;
while(rear_x[son_x][son_y]!=son_x || rear_y[son_x][son_y]!=son_y)
{
printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,son_x,son_y,rear_x[son_x][son_y],rear_y[son_x][son_y]);
xx=son_x; yy=son_y;
son_x=rear_x[xx][yy];
son_y=rear_y[xx][yy];
if(Map[son_x][son_y]!='.' && Map[son_x][son_y]!='X')
for(int i=;i<=(Map[son_x][son_y]-'');i++)
printf("%ds:FIGHT AT (%d,%d)\n",cnt++,son_x,son_y);
}
printf("FINISH\n");
return ;
}
for(int i=;i<;i++)
{
node next; int x,y;
next.x=x=cur.x+dir[i][];
next.y=y=cur.y+dir[i][];
if(<=x && x<n && <=y && y<m && visit[x][y]== && Map[x][y]!='X')
//只被更新一次,与优先队列优化的Dijkstra的区别在于
//更新点到每个有可能更新它的点的权值是固定的,所以弹出来最短的点,然后更新,就是最短的。
//优先队列优化的Dijkstra,它多次被松弛的原因是在于弹出来的点是最短路径。但是
//更新点可能有多条路径到达更新点,假设更新点是最后一个点,那么倒数第二个点到这个更新点的权值不一样,
//所以每条路径上的到倒数第二个点的最短路径加倒数第二个点与倒数第一个点的边的权值就可能会让更新点被多次更新
//然而如果在类似于hdu1026,这样的图中,由于更新点到每个有可能更新它的点的权值是固定的,所以
//只需要用到倒数第二个点的最短路径去更新它即可,从优先队列里弹出来的点就是到达每个点的最短路径,
//所以只需要把bfs中的普通队列换成优先队列即可。
{
if(Map[x][y]=='.')
{
next.dist=cur.dist+;
pq.push(next);
pre_x[next.x][next.y]=cur.x;
pre_y[next.x][next.y]=cur.y;
visit[next.x][next.y]=;
}
else
{
int step=Map[x][y]-''+;
next.dist=cur.dist+step;
pq.push(next);
pre_x[next.x][next.y]=cur.x;
pre_y[next.x][next.y]=cur.y;
visit[next.x][next.y]=;
}
}
}
}
return ;
} int main()
{
// freopen("test.txt","r",stdin);
while(~scanf("%d%d%*c",&n,&m))
{
init();
for(int i=;i<n;i++)
{
scanf("%s",Map[i]);
}
node start;
start.x=;
start.y=;
start.dist=;
if(bfs(start)==)
printf("God please help our poor hero.\nFINISH\n"); }
return ;
}

hdu1026(bfs+优先队列+打印路径)的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

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

  4. h1026 BFS(打印x与路径)

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

  5. Codeforces 3A-Shortest path of the king(BFS打印路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  6. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  7. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  8. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  9. UVA-10480-Sabotage(最大流最小割,打印路径)

    链接: https://vjudge.net/problem/UVA-10480 题意: The regime of a small but wealthy dictatorship has been ...

随机推荐

  1. [codeforces538E]Demiurges Play Again

    [codeforces538E]Demiurges Play Again 试题描述 Demiurges Shambambukli and Mazukta love to watch the games ...

  2. POJ 2375 Cow Ski Area【tarjan】

    题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经 ...

  3. K/3Cloud二次开发基于WebDev附加进程调试

    大部分人在进行K/3cloud二次开发插件的调试时,选择的是附加IIS进程w3wp调试,本文给大家介绍一下基于WebDev附加进程调试,不用重启iis. 步骤如下: 1)拷贝K/3cloud产品安装目 ...

  4. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  5. mysql 常用管理命令

    常见的管理mysql命令 (1)用于选择在MySQL工作区指定的数据库(选择数据库): USE Databasename; (2)列出了MySQL数据库管理系统中的所有可访问的数据库: SHOW DA ...

  6. [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3629  Solved: 1432[Submit][Sta ...

  7. MongoDB学习day08--mongoose预定义修饰符和getter、setter修饰符

    一.mongoose预定义修饰符 lowercase. uppercase . trim var UserSchema=mongoose.Schema({ name:{ type:String, tr ...

  8. Eclipse-Java代码规范和质量检查插件-Checkstyle

    CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具.它能够自动化代码规范检查过程,从而使得开发人员从这项重要但枯燥的任务中解脱出来.它可以根 ...

  9. 学习swift从青铜到王者之swift闭包06

    语法表达式 一般形式:{ (parameters) -> returnType in statements } 这里的参数(parameters),可以是in-out(输入输出参数),但不能设定 ...

  10. Windows平台下Git(gitblit)服务器搭建

    环境:Windows 10 专业版32位 因为公司服务器上已经搭了Visual SVN等,只好在Windows上搭个Git Server给大家用. 参考链接:http://www.cnblogs.co ...