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. 09.13随笔2014年9月13日22:32:38,奶爸的英语教室,groovy

    我们这里只推荐一本语法书:台湾的旋元佑老师写的<文法俱乐部>(简体版名为<语法俱乐部>).这本书因为出版社倒闭而绝版,淘宝可以买到影印的版本. (1)学英语到 奶爸的英语教室  ...

  2. 编程语言的发展趋势by Anders Hejlsberg

    这是Anders Hejlsberg在比利时TechDays 2010所做的开场演讲. 编程语言的发展非常缓慢,期间也当然出现了一些东西,例如面向对象等等,你可能会想,那么我么这么多年的努力都到哪里去 ...

  3. 《精通CSS-高级Web标准解决方案》阅读计划

    第一周     第1章 基础知识 1 第2章 为样式找到应用目标 1 第3章 可视化格式模型 1 第4章 背景图像效果 1       第二周     第5章 对链接应用样式 1 第6章 对列表应用样 ...

  4. Linux网络设置高级指南

    from:http://www.oschina.net/question/23734_117144 Linux网络设置高级指南 本文面向的是被Linux复杂的有线无线网络架构弄得头昏脑胀:或者被网上半 ...

  5. a标签使用

    1.发起邮件 注意:如果mailto后面同时有多个参数的话,第一个参数必须以“?”开头,后面的参数每一个都以“&”分隔. <a href="mailto:281345774@q ...

  6. Jquery EasyUI中treegrid的中右键菜单和一般按钮同时绑定事件时的怪异事件

    做个项目使用jquery  easyui来做前端,也许是对此不是很熟悉,总是发现一些不可理解的事件. 主要源代码如下: <script type="text/javascript&qu ...

  7. 《Excel图表之道》读书笔记

    一.突破常规的作图方法 突破Excel的默认颜色 非数据元素用淡色 突破Excel的图表布局 图表要素:主标题.副标题.图例.绘图.脚注 竖向构图 标明数据来源.图表注释.坐标轴截断符号 专业的水蓝色 ...

  8. [转]left join,right join,inner join区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  9. php实现手机拍照上传头像功能

    现在手机拍照很火,那么如何使用手机拍照并上传头像呢?原因很简单,就是数据传递,首先手机传递照片信息,这个就不是post传递 也不是get函数传递, 这个另外一种数据格式传递,使用的是$GLOBALS ...

  10. hdu 5612 Baby Ming and Matrix games

    Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...