zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649
Rescue
Time Limit: 2 Seconds Memory Limit: 65536 KB
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8 
#.#####. 
#.a#..r. 
#..#x... 
..#..#.# 
#...##.. 
.#...... 
........
Sample Output
13
题意:从'r'起步,目的地为'a','#'不可过,'.'为一步,特殊的是‘x'算两步。求最少步数。
思路:bfs。这里需要注意的是x,因为算两步,在普通的bfs队列里不一定先出列的是最短步,所以得用优先队列。
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff struct node{
int x,y,step;
bool operator < (const node &r) const{
return step>r.step;
}
}tmp; priority_queue<node> qu;
int n,m,x1,y1_;
char matrix[][];
bool b[][];
char xy[][]={{,,,-},{,-,,}}; inline bool Check(int x,int y);
int Bfs(); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
while(~scanf("%d %d",&n,&m)){
for(int i=;i<=m+;i++) matrix[][i]='#',matrix[n+][i]='#';
for(int i=;i<=n+;i++) matrix[i][]='#',matrix[i][m+]='#';
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
matrix[i][j]=getchar();
if(matrix[i][j]=='r') x1=i,y1_=j;
}
}
int ans=Bfs();
if(ans==-) puts("Poor ANGEL has to stay in the prison all his life.");
else printf("%d\n",ans);
}
return ;
}
inline bool Check(int x,int y){
return matrix[x][y]!='#'&&b[x][y]==;
}
int Bfs(){
memset(b,,sizeof(b));
while(!qu.empty()) qu.pop();
tmp.step=;
tmp.x=x1;
tmp.y=y1_;
qu.push(tmp);
b[x1][y1_]=;
while(!qu.empty()){
tmp=qu.top();
qu.pop();
int x=tmp.x,y=tmp.y;
int tx,ty,ts=tmp.step+;
for(int i=;i<;i++){
tx=x+xy[][i];
ty=y+xy[][i];
if(Check(tx,ty)){
if(matrix[tx][ty]=='.'){
b[tx][ty]=;
tmp.x=tx;
tmp.y=ty;
tmp.step=ts;
qu.push(tmp);
}else if(matrix[tx][ty]=='x'){
b[tx][ty]=;
tmp.x=tx;
tmp.y=ty;
tmp.step=ts+;
qu.push(tmp);
}else{
return ts;
}
}
}
}
return -;
}
zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】的更多相关文章
- Rescue HDU1242 (BFS+优先队列)                                                                                                         标签:               搜索                                            2016-05-04 22:21             69人阅读              评论(0)
		
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
 - HDU 1242 -Rescue  (双向BFS)&&( BFS+优先队列)
		
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
 - BFS+优先队列+状态压缩DP+TSP
		
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
 - hdu 2102 A计划 具体题解 (BFS+优先队列)
		
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
 - POJ 1724 ROADS(BFS+优先队列)
		
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
 - hdu 1242 找到朋友最短的时间  (BFS+优先队列)
		
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
 - HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
		
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
 - hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
		
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
 - POJ - 2312 Battle City BFS+优先队列
		
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
 - D. Lunar New Year and a Wander bfs+优先队列
		
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
 
随机推荐
- poj 2229 Sumsets(dp)
			
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...
 - IntelliJ IDEA小问题解决方法------(持续更新)
			
1:IDEA运行时报错提示“找不到或无法加载主类”:在确保IDEA开发环境无误后->file->invalidate Cache/restart,之后再重新build.问题解决. 2.如何 ...
 - 模拟估算器:scikit-learn Estimator
			
转载:https://www.toutiao.com/i6606193174010397187/ 当一个数据科学项目刚刚开始时,关键是要尽可能快地走向一个最小可行的产品(MVP).这个MVP将包含最终 ...
 - cmd命令总结
			
1.进入d盘 2.命令运行D:\mongodb\bin中的mongod.exe 显示安装成功!(命令行下运行 MongoDB 服务器)
 - php metaphone()函数解析
			
php metaphone() 函数计算字符串的 metaphone 键,本文章向码农们介绍 php metaphone() 函数的基本用法和实例,需要的码农可以参考一下本文章的方法和实例. 定义和用 ...
 - Python中的logger和handler到底是个什么鬼
			
最近的任务经常涉及到日志的记录,特意去又学了一遍logging的记录方法.跟java一样,python的日志记录也是比较繁琐的一件事,在写一条记录之前,要写好多东西.典型的日志记录的步骤是这样的: 创 ...
 - django项目环境设置
			
1.连接数据库,如mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_comi ...
 - C编程常错项
			
linux系统下C编程一般报错;1,使用sqrt开平方函数未定义,是因为math.h[库,头文件未包含]2,隐式申明与内建函数"XXXX"不兼容;上述问题所在,一般是因为使用exi ...
 - jquery函数写法
			
普通jquery函数写法 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script&g ...
 - 用yield 实现协程 (包子模型)
			
协程是一种轻量级的线程 无需线程上下级的开销, 所有的协程都在一个线程内执行 import time def consumer(name): print('%s is start to eat bao ...