Problem Description

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

又是一道经典的深搜题,做了1180题再来做这个,就是秒A啊。

说下题意:



天使被关在了一个监狱里面,a代表天使,r代表天使的朋友,x是警卫,#是墙。r可以杀死所以的警卫,杀一个警卫需要1个单位时间,移动一步也需要一个单位时间。问,天使的朋友r最短需要多久才可以把天使a救出来。

如果不能救出天使,输出:Poor ANGEL has to stay in the prison all his life.

#include <iostream>
#include <cstdio>
#include <cstring>
#include<queue>
using namespace std;
struct node{
int x,y,t;
friend bool operator<(node n1,node n2){
return n2.t<n1.t;
}
};
int n,m;
char map[210][210];
int d[210][210];
int ax,ay,rx,ry;
node ft;
int mx[]={1,0,-1,0};
int my[]={0,-1,0,1}; int judge(int x,int y){
if(x<0||x>=n||y<0||y>=m){
return 0;
}
if(map[x][y]=='#'){
return 0;
}
if(d[x][y]){
return 0;
}
return 1;
} void bfs(){
priority_queue<node> q;
ft.x=rx;
ft.y=ry;
ft.t=0;
q.push(ft);
d[rx][ry]=1;
while(!q.empty()){
ft=q.top();
q.pop();
//printf("%d,%d,%d\n",ft.x,ft.y,ft.t);
int x=ft.x;
int y=ft.y;
if(x==ax&&y==ay){
printf("%d\n",ft.t);
return;
}
for(int i=0;i<4;i++){
x=ft.x+mx[i];
y=ft.y+my[i];
if(!judge(x,y)){
continue;
}
node nt;
if(map[x][y]=='.'||map[x][y]=='a'){
nt.x=x;
nt.y=y;
nt.t=ft.t+1;
d[x][y]=1;
q.push(nt);
}else if(map[x][y]=='x'){
nt.x=x;
nt.y=y;
nt.t=ft.t+2;
d[x][y]=1;
q.push(nt);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
} int main()
{
while(~scanf("%d%d",&n,&m)){
memset(d,0,sizeof(d));
for(int i=0;i<n;i++){
scanf("%s",map[i]);
for(int j=0;j<m;j++){
if(map[i][j]=='a'){
ax=i,ay=j;
}
if(map[i][j]=='r'){
rx=i,ry=j;
}
}
}
bfs();
}
return 0;
}

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)的更多相关文章

  1. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  2. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

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

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

  4. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  5. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

  6. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  7. HDU 1242 Rescue (BFS+优先队列)

    题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...

  8. hdu 1242 Rescue(BFS入门)

    第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...

  9. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

随机推荐

  1. 【CMD】findstr命令

    findstr用来搜索匹配字符串的文件. FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [ ...

  2. BufferedReader、FileReader、FileInputStream的区别

    一.BufferReader BufferedReader   由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取分行文本很适合,BufferedReade ...

  3. bootstrap Tooltip换行问题

    bootstrap自身带有tooltip,使用起来很方便,但是美中不足,它的tooltip并不支持换行. 比如我们通过<textarea>输入框传入到数据库的长文本,文本是带有换行符的,但 ...

  4. XML2_XML的节点和元素

    在JAVA语言中使用JAXP操作XML文件的时候,有两个接口,一个是Node,一个是Element,Element接口继承自Node接口. 在这一层次我们进一步理解XML中更具体的分类: 元素,属性, ...

  5. mongodb3.2系统性学习——5、游标 模糊查询 findAndModify函数

    1首先介绍查询结果 返回的过程: 进行查询的时候mongodb 并不是一次哪个返回结果集合的所有文档,而是以多条文档的形式分批返回查询的结果,返回文档到内存中. 好处: 减少了客户端与服务器端的查询负 ...

  6. Rsync和FastDFS

    在做分布式文件存储的时候,常常用到两个工具,Rsync和FastDFS:这两者本质的区别在于前者的实时性相面相对较差,需要手工编写脚本同步,然后在放到定时任务(cron)中:FastDFS自动实现同组 ...

  7. asp.net core 认证及简单集群

    众所周知,在Asp.net WebAPI中,认证是通过AuthenticationFilter过滤器实现的,我们通常的做法是自定义AuthenticationFilter,实现认证逻辑,认证通过,继续 ...

  8. 定位 -CLGeocoder - 编码

    #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...

  9. java+eclipse+tomcat+mysql+jdbc——完美配置攻略

    说明: 软件均采用最新版本,请大家详细阅读,注意每个细节,无需分门别类的百度各种教程,配置java环境这一篇就够了. 所需软件及版本(参考): java8; - jdk1.8.0_60; - jre1 ...

  10. 网络流系列算法总结(bzoj 3438 1061)

    网络流嘛,怎么看都是一堆逗逼题嘛,反正遇到还是都做不起嘛.... 网络流的模板非常简单,难点都在于建图,网络流的建图解决问题范围之广,下至A+B Problem,上至单纯形,线性规划.所以如果对于网络 ...