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 ...
随机推荐
- CSU1030素数槽
Description 处于相邻的两个素数p和p + n之间的n - 1个连续的合数所组成的序列我们将其称为长度为n的素数槽.例如,‹24, 25, 26, 27, 28›是处于素数23和素数29之间 ...
- MTK android 重启测试脚本
@echo off set reboot_time=0 :start call adb -s 0123456789ABCDEF reboot set DATESTAMP=%DATE% set TIME ...
- less的配置和使用
less 的配置: 1.浏览器端的使用 <link rel="stylesheet/less" type="text/css" href="st ...
- 洛谷——P2434 [SDOI2005]区间
P2434 [SDOI2005]区间 题目描述 现给定n个闭区间[ai, bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间 ...
- chrome插件vimium的安装和使用
vimium工具的作用:使你脱离鼠标,使用键盘方便操作页面,默认对所有网站生效 1.chrome商店里有的,但是,我怎么安装,都不行 2.源码安装:http://vimium.github.io/ h ...
- 7.1 itertools--高效循环的创建函数
7. 函数式编程库 本库主要提供了支持函数式编程的函数和类,以及提供通用调用对象. 7.1 itertools--高效循环的创建函数 本模块主要提供了迭代器方面的操作函数,跟语言API.Haskell ...
- axis2开发webservice之编写Axis2模块(Module)
axis2中的模块化开发.能够让开发者自由的加入自己所需的模块.提高开发效率,减少开发的难度. Axis2能够通过模块(Module)进行扩展. Axis2模块至少须要有两个类,这两个类分别实现了Mo ...
- 在 Ubuntu 开启 GO 程序编译之旅
本文将使用 putty 连接到一台阿里云 Ubuntu 16.04 服务器,在其上安装 go 语言的编译环境,旨在呈现从安装到"你好,世界!"涉及的方方面面,希望完成这个过程无须觅 ...
- 遍历数据库全部表,将是datetime类型的列的值进行更新
declare @tablename nvarchar(80) declare @cloumn nvarchar(80) declare @sql nvarchar(400) declare ...
- 使用PowerShell 创建SharePoint 站点
使用PowerShell 创建SharePoint 站点 在SharePoint开发中,你应该学会使用PowerShell管理SharePoint网站.SharePoint Manag ...