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. ASP.NET MVC 5 入门教程 (2) 控制器Controller

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-controller.html 上一节:ASP.NET MVC ...

  2. 【逆向怎么玩】 动态调试一款牛逼C++ IDE实录

    声明 本篇只从逆向兴趣出发,研究其程序运行原理. CLion程序版权为jetBrains所有. 注册码授权为jetBrains及其付费用户所有. 不会释出任何完整的源代码. 涉及能直接推算出注册码的地 ...

  3. 在Ubuntu-14.04.3配置并成功编译Android6_r1源码

    折腾了一周,终于把Android6_r1的源码编译成功.先上图,这是在ubuntu中运行的Android模拟器: 由于我是在win8中安装虚拟机VMware,然后在虚拟机中安装Ubuntu进行编译,所 ...

  4. Orchard使用Tags(标签)组织文本

    本文链接:http://www.cnblogs.com/souther/p/4517476.html 主目录 原文链接:http://docs.orchardproject.net/Documenta ...

  5. Git.Framework 框架随手记-- 分享一个"比较垃圾"的项目

    本文主要分享一个Git.Framework 开发的一个项目的部分源码,此项目代码"比较垃圾",所以请各位码农,码畜,码神,码圣勿喷!发此文只为记录工作问题以及分享问题! 一. 项目 ...

  6. [poj3046]Ant Counting(母函数)

    题意: S<=x1+x2+...+xT<=B 0<=x1<=N1 0<=x2<=N2 ... 0<=xT<=NT 求这个不等式方程组的解的个数. 分析: ...

  7. ThinkPHP之项目搭建

    前言 在做javaweb开发时,我们创建一个项目,MyEclipse会自动为我们生成相应的目录结构,我们在此结构上才进行web开发,在使用TinkPHP框架做PHP开发时,一样,我们如何生成一个标准的 ...

  8. [AaronYang]C#人爱学不学[3]

    本文章不适合入门,只适合有一定基础的人看.我更相信知识细节见高低,我是从4.0开始学的,终于有时间系统的学习C#5.0,是5.0中的知识,会特殊标记下.但写的内容也可能含有其他版本framework的 ...

  9. [c#基础]堆和栈

    前言 堆与栈对于理解.NET中的内存管理.垃圾回收.错误和异常.调试与日志有很大的帮助.垃圾回收的机制使程序员从复杂的内存管理中解脱出来,虽然绝大多数的C#程序并不需要程序员手动管理内存,但这并不代表 ...

  10. redmine邮件发送功能配置详解

    redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...