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 优先队列】的更多相关文章

  1. 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 ...

  2. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  3. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  4. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  5. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  6. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  7. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  8. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  9. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  10. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

随机推荐

  1. ios关闭自动更新

    iPhone系统更新超级烦人,避免测试机升级的方法 1. 设置禁用网络 设置-网线局域网-使用WLAN与蜂窝移动网的应用,将设置项设置为关闭 2. 一劳永逸,安装证书, 证书https://oldca ...

  2. 数组比较大小的几种方法及math是方法

    call apply bind 的区别? 解决函数内this的指向: 1.可以在函数外提前声明变量 一般情况下我们用   var _this/that=this 2.通过apply和call来修改函数 ...

  3. sqoop操作之HDFS导出到ORACLE

    注意:在导出前需要先创建待导出的表结构.如果导出的表在数据库中不存在则会报错:如果重复导出多次,表中的数据会重复: ; ; 导出表的所有字段 sqoop export --connect jdbc:o ...

  4. Python3 open()函数参数

    open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=No ...

  5. fio与dd测试结果记录

    以下测试基于win7内安装的vbox虚机内进行. vbox-vm挂载了7.2k disk作为本地系统盘,挂载了ssd 8G空间作为mount /mnt/data /dev/sdb 今天顺便了做个一个简 ...

  6. dockers的容器删除

    1.停用全部运行中的容器: docker stop $(docker ps -q) 2.删除全部容器: docker rm $(docker ps -aq) 3.一条命令实现停用并删除容器: dock ...

  7. 模板方法模式( TemplateMethod)

    定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤. AbstractClass 是抽象类,其实也是一个抽象模板,定义并实现 ...

  8. 装饰模式 (Decoratory)

    动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活. 装饰模式就是利用 SetComponent 来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个 ...

  9. Dep数据发布,推送

    package com.cfets.ts.u.shchgateway.util; import com.cfets.cwap.s.stp.MessageUnit; import com.cfets.t ...

  10. uva-331-枚举-交换的方案数

    题意:冒泡排序,最小交换数的前提下有多少用方案把数组变成从小到大的顺序, 注意: 3 2 1 3的下表是1  2的是2 1的是3  交换 3 2,那么第一个交换数是1 最小交换数=逆序数的和 那么,只 ...