1039: Rescue

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 1320  Solved: 306

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 

用优先队列+bfs,写得蛮有逻辑性(哈哈):

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define MAX_N 205
using namespace std;
struct Node
{
int x,y,t;
bool operator<(const Node &a)const
{
return t>a.t;
}
}; Node start; char map[MAX_N][MAX_N]; int dir[][]={{,},{,-},{,},{-,}}; int n,m; int bfs()
{
priority_queue<Node> que;
Node cur,next;
int i,j;
map[start.x][start.y]='#';
que.push(start);
while(!que.empty())
{
cur=que.top();
que.pop();
for(i=;i<;i++)
{
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
next.t=cur.t+; if(next.x<||next.x>=n||next.y<||next.y>=m)
continue;
if(map[next.x][next.y]=='#')
continue;
if(map[next.x][next.y]=='r')
return next.t;
if(map[next.x][next.y]=='x')
{
next.t++;
map[next.x][next.y]='#';
que.push(next);
}
else if(map[next.x][next.y]=='.')
{
map[next.x][next.y]='#';
que.push(next);
}
}
}
return -;
} int main()
{
// freopen("a.txt","r",stdin);
int i,ans;
char *p;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%s",map[i]);
if(p=strchr(map[i],'a'))
{
start.x=i;
start.y=p-map[i];
start.t=;
}
}
ans=bfs(); printf(ans+?"%d\n":"Poor ANGEL has to stay in the prison all his life.\n",ans);
}
return ;
}

AC

Acknowledge:罗里罗嗦夫斯基     http://blog.chinaunix.net/uid-21712186-id-1818266.html(我写的“优先队列”随笔也从那借鉴了蛮多)

Rescue的更多相关文章

  1. Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)

    本节我们讨论 Suspend/Resume 和 Rescue/Unrescue 这两组操作. Suspend/Resume 有时需要长时间暂停 instance,可以通过 Suspend 操作将 in ...

  2. HDU-4057 Rescue the Rabbit(AC自动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. 使用Grub Rescue恢复Ubuntu引导

    装了Ubuntu和Window双系统的电脑,通常会使用Ubuntu的Grub2进行引导. Grub2会在MBR写入引导记录,并将引导文件放在/boot/grub,破坏任意一项都会导致系统无法正常启动. ...

  4. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  5. [转]linux援救模式:linux rescue使用详细图解

    网上很多网友问怎么进rescue 模式,不知道怎么用rescue来挽救系统.  现在我来图解进入rescue (示例系统为RHEL 3) 1.用安装光盘或者硬盘安装的方式进入安装界面,在shell 中 ...

  6. 安装了ubuntu14.04+windows7双系统的笔记本启动后出现grub rescue>提示符

    解决思想如下: 1.在grub rescue>提示符处输入ls  即可看到该命令列出了硬盘上的所有分区,找到安装了linux的分区,我的安装在(hd0,msdos8)下,所以我以(hd0,msd ...

  7. Win7启动修复(Ubuntu删除后进入grub rescue的情况)

    起因:装了win7,然后在另一个分区里装了Ubuntu.后来格掉了Ubuntu所在的分区.系统启动后出现命令窗口:grub rescue:_ 正确的解决方式: 1.光驱插入win7安装盘或者用USB启 ...

  8. HDU1242 Rescue

    Rescue Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Description A ...

  9. 山东省第四届acm.Rescue The Princess(数学推导)

    Rescue The Princess Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 412  Solved: 168 [Submit][Status ...

随机推荐

  1. android软键盘的一些控制 转来的,格式有点乱

    "EditText + Button"  形成一个 "输入+按键响应" 的案例在android编程中是最常见不过的了. 但还有一些细节需要注意: 在EditTe ...

  2. 怎样写 OpenStack Neutron 的 Extension (三)

    通过上几章的介绍,我们现在的 myplugin 文件夹看上去应该是这样的: - neutron/ - plugins/ - myplugin/ - __init__.py - plugin.py - ...

  3. [BZOJ 1797][AHOI2009]最小割(最小割关键边的判断)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1797 分析: 在残余网络中看: 对于第1问: 1.首先这个边必须是满流 2.其次这个边 ...

  4. Javascript基础系列之(五)条件语句(比较操作符)

    和其他语言一样,JavaScript也有条件语句对流程上进行判断.包括各种操作符合逻辑语句 比较操作符 常用的比较操作符有      等于 == ,  不等于!= , 大于 >, 小于 < ...

  5. angular_$inject

    <!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> &l ...

  6. 在eclipse中安装插件

    1.在Eclipse中菜单help选项中选择install new software选项, 2.在work with 栏中输入 http://download.eclipse.org/releases ...

  7. PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  8. Linux下SVN安装配置

      第一章 安装 1. 采用源文件编译安装.源文件共两个,为:subversion-1.6.1.tar.gz (subversion 源文件)subversion-deps-1.6.1.tar.gz ...

  9. iOS开发小技巧--利用苹果官方API播放视频(方法已经过时,了解一下)

  10. word-break:brea-all;word-wrap:break-word的区别

    //form==>http://www.cnblogs.com/2050/archive/2012/08/10/2632256.html <p style="background ...