hdu1026(bfs+优先队列+打印路径)
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
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 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.
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.
.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.
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
#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+优先队列+打印路径)的更多相关文章
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜
此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 ...
- h1026 BFS(打印x与路径)
题意: Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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 ...
- BFS+打印路径
题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- UVA-10480-Sabotage(最大流最小割,打印路径)
链接: https://vjudge.net/problem/UVA-10480 题意: The regime of a small but wealthy dictatorship has been ...
随机推荐
- CodeForces - 356A Knight Tournament
http://codeforces.com/problemset/problem/356/A 首先理解题意 每次给出l 和r 在l - r之间还有资格的选手中得出一个胜者 暴力思路: 首先维护还有资 ...
- Codeforces 848C (cdq分治)
Codeforces 848C Goodbye Souvenir Problem : 给一个长度为n的序列,有q个询问.一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点 ...
- 洛谷——P2068 统计和
P2068 统计和 题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=1000 ...
- openstack swift middleware开发
首先MiddleWare核心代码,这段代码卸载swift的源代码目录下,~/swift/swift/common/middleware下新建deletionpreventing.py: import ...
- struts2 动态工作流
话不多说,直接贴代码: public class TestAction { private String nextPage;//保存下一步内容的属性 public String destroy(){ ...
- [React] {svg, css module, sass} support in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr ...
- C语言必会面试题(3、耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)
3.耶稣有13个门徒.当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1.2,3.1,2,3.... 凡是报到"3"就退出圈子.最后留在圈子 ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...
- LINQ体验(1)——Visual Studio 2008新特性
一.写本系列的目的 我平时利用课余零碎时间来学习ASP.NET3.5.LINQ.Silverlight.ASP.NET 3.5 Extensions等新东西,通过笔记形式来记录自己所学的历 程.也给大 ...
- ubuntu hadoop伪分布式部署
环境 ubuntu hadoop2.8.1 java1.8 1.配置java1.8 2.配置ssh免密登录 3.hadoop配置 环境变量 配置hadoop环境文件hadoop-env.sh core ...