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点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- python学习笔记--装饰器
1.首先是一个很无聊的函数,实现了两个数的加法运算: def f(x,y): print x+y f(2,3) 输出结果也ok 5 2.可是这时候我们感觉输出结果太单一了点,想让代码的输出多一点看起来 ...
- 小峰mybatis(4)mybatis使用注解配置sql映射器
主流开发还是使用xml来配置:使用注解配置比较快,但是不支持所有功能:有些功能还是得用配置文件: 一.基本映射语句: @Inert @Update @Delete @Select 二.结果集映射语句 ...
- Oracle学习操作(3)
一.if条件语句 set serverout on; ; v ):='world'; begin dbms_output.put_line('hello'||n||v); end; / hello1w ...
- JavaScript之图片操作2
在前一次,我们实现最简单的图片切换效果,这一次,依旧还是图片切换,具体效果如下: 通过点击下面的小图,上面的大图和标题随之切换.因此,我们需要三个容器分别放标题,大图和小图. <!--大图描述- ...
- JQuery基础(选择器、事件、DOM操作)
一.选择器 1.基本选择器 ①id选择器 ②class选择器 ③标签名选择 ④并列选择 ⑤后代选择 代码用法展示: <title></tit ...
- RDD之四:Value型Transformation算子
处理数据类型为Value型的Transformation算子可以根据RDD变换算子的输入分区与输出分区关系分为以下几种类型: 1)输入分区与输出分区一对一型 2)输入分区与输出分区多对一型 3)输入分 ...
- sklearn 线性模型使用入门
LinearRegression fits a linear model with coefficients to minimize the residual sum of squares betw ...
- 本地管理表空间和字典管理表空间的特点,ASSM有什么特点
字典管理表空间(Dictionary-Managed Tablespace简称DMT),8i以前包括以后都还可以使用的一种表空间管理模式,通过数据字典管理表空间的空间使用. Oracle使用两个字典来 ...
- NVMe on RHEL7
原文地址https://www.dell.com/support/article/cn/zh/cnbsd1/sln312382/nvme-on-rhel7?lang=en Posted on beha ...
- RESTful转载,多看几遍就理解了写点自己的看法和理解
要理解资源路由就要理解什么是RESTful.如果一个架构符合REST(即Representational State Transfer的缩写,意为表现层状态转化)原则,就称它为RESTful架构. R ...