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图片加载框架

    本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...

  2. "互联网思维"背后的谎言

    互联网公司/思维是什么鬼,说来惭愧上学的时候还因为知道www(World Wide Web)的中文名自豪了好久,之后在”高等学府“里学习软件工程,还愚蠢的以为自己步入了互联网之门. internet嘛 ...

  3. Cordova4.0 系列 -- 基本环境搭建(1)

    一. 安装Node.js基本环境 官网下载地址:https://nodejs.org/ 安装成功之后可以使用简单命令查看其版本 node -v npm相关命令 node cli.js install ...

  4. mac点滴

    1.8个常用的功能. 转载:http://www.tuicool.com/articles/BfEjQzr 2.把pwd复制到剪切板?  pwd  |  pbcopy 有时间看看这个 http://j ...

  5. ajax请求模拟登录

    前台 @if (Session["username"] != null) { <div class="login"> <span style= ...

  6. 第三章:Javascript类型、值和变量。

    计算机程序的运行需要对值(value)比如数字3.14或者文本"hello world"进行操作,在编程语言中,能够表示并操作的值的类型叫做数据类型(type),编程语言最基本的特 ...

  7. java设计优化--装饰者模式

    装饰者模式拥有一个设计非常巧妙的结构,它可以动态的添加功能.在基本的设计原则中,有一条重要的设计准则就是合成/聚合复用原则.根据该原则的思想,代码复用应该尽可能使用委托,而不是使用继承.因为继承是一种 ...

  8. git 的简单使用方法

    git 的简单使用方法1. 服务器 安装完成2. ssh 中的账号创建完成3. 创建 ssh 账号,会在 ssh 的安装目录下的home 目录里面,多了用户家目录4. 进入该目录 ,创建一个新的文件夹 ...

  9. Html-Css-a标签的使用

    a标签去掉下划线 a{ text-decoration:none; } 或者把这个属性分别加到a标签下, a:link{ text-decoration:none; } a:visited{ text ...

  10. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...