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 ...
随机推荐
- php实现设计模式————单例模式
php实现设计模式————单例模式 什么是单例模式 为什么要使用单例模式 php中有哪些方式实现新建一个对象实例 如何阻止这种实例化实现理想的单例模式 代码实现 什么是单例模式 为什么要使用单例模式 ...
- [luoguP1866]滑动窗口(单调队列)
传送门 可以搞2个单调队列. 然后,然后就没有然后了. # include <iostream> # include <cstdio> # include <cstrin ...
- Pollard rho模板
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #in ...
- 进程Queue、线程Queue、堆栈、生产者消费者模型
没学队列之前,可以用文件实现进程之间通信 但是有2个问题: 1. 速度慢:文件是保存在硬盘空间 2. 为了数据安全要加锁(处理锁是一件很麻烦的事,容易死锁,建议自己轻易不要处理锁) 队列:队列是基于管 ...
- mongodb按照日期分组统计
目录 1.使用时间格式化方法 2.进行时间补偿(默认当前时区是东八区,即8x3600x1000=28800000) mongodb的默认时间是格林尼治时间,如果是要按照日期进行分组需要注意!!!. 解 ...
- FTP操作类的使用
FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...
- C# 读自己的资源文件
Assembly assm = this.GetType().Assembly;//Assembly.LoadFrom(程序集路径); foreach (string resName in assm. ...
- SpringBoot初始教程之日志处理(二)
SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...
- Deepin-安装node
点击下载:Linux x64 文件解压: 方式1$xz -d file.tar.xz $tar -xvf file.tar 方式2 $tar xvJf file.tar.xz 解压后,把它移动到:/u ...
- python 多线程中同步的小样例
#!/usr/bin/python # -*- coding: UTF-8 -*- # 在一个资源池中.获取资源 # Author: zhang # Date: 2015-7-27 import ti ...