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. EF实体框架之CodeFirst三

    前两篇博客学习了数据库映射和表映射,今天学习下数据库初始化.种子数据.EF执行sql以及执行存储过程这几个知识. 一.数据库初始化策略 数据库初始化有4种策略 策略一:数据库不存在时重新创建数据库 D ...

  2. C++成员权限控制(总结)

    1) 前言 在我学习C++的过程中,类中成员的权限控制一直是比较头疼的一个点,一会public,一会又private,还有protected,再加点继承,而且又有公有继承.私有继承,保护继承,所以感觉 ...

  3. JavaScript和html5 canvas生成圆形印章

    代码: function createSeal(id,company,name){ var canvas = document.getElementById(id); var context = ca ...

  4. [codevs 1306]广播操的游戏(Trie)

    题目:http://codevs.cn/problem/1306/ 分析:题意一看就知道就是要求Trie有多少个节点.但是如果每次单独取原串的所有子串加入Trie会超时,为什么呢?比方说AAABBBC ...

  5. Coding the Matrix (3):矩阵

    1. 矩阵与映射 矩阵和映射包含两方面的关系: 简单:已知矩阵 M, 从向量 x 映射到 M * x. (注:矩阵与行向量的点乘) 稍微复杂:已知映射 x ->M * x, 求矩阵 M. 第一种 ...

  6. JavaScript、jQuery、AJAX、JSON 解释

  7. struts2动态方法

    动态方法调用 在Struts2中动态方法调用有三种方式,动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多 第一种方式:指定method属性 这种方式我们前面已经用到过, ...

  8. js获取服务器时间戳

    <!DOCTYPE html> <html> <head> <title>ajax</title> </head> <bo ...

  9. 【Matplotlib】 标注一些点

    相关的文档: Annotating axis annotate() command 标注的代码如下: ... t = 2 * np.pi / 3 plt.plot([t, t], [0, np.cos ...

  10. 粒子群优化算法-python实现

    PSOIndividual.py import numpy as np import ObjFunction import copy class PSOIndividual: ''' individu ...