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


本题题意是从‘a’搜到‘r’,‘.’为路1次为1秒,‘x’为卫兵,1次2秒,问从‘a’到‘r’用最短时间是多少。

处理时的方法是bfs,由于有卫兵的存在,队列的时间可能不是递增的,这样出队求最小值就可能不对,所以采取的是优先队列,每次让最短时间的出队。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std; struct node
{
int x, y, tot;
bool operator < (const node &a)const
{
return tot>a.tot;
}
};
int n, m;
node start;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
char mp[205][205];
int vis[205][205];
bool cheak(int x, int y)
{
if (x >= 1 && x <= m&&y >= 1 && y <= n&&mp[x][y] != '#')
return 1;
else
return 0;
}
int bfs()
{
priority_queue<node>q;
node cur, next;
mp[start.x][start.y] = '#';
q.push(start);
while (!q.empty())
{
cur = q.top();
q.pop();
for (int i = 0; i < 4; i++)
{
next.x = cur.x + dir[i][0];
next.y = cur.y + dir[i][1];
next.tot = cur.tot + 1;
if (cheak(next.x, next.y))
{
if (mp[next.x][next.y] == 'r')
return next.tot;
else if (mp[next.x][next.y] == '.')
{
mp[next.x][next.y] = '#';
q.push(next);
}
else if (mp[next.x][next.y] == 'x')
{
mp[next.x][next.y] = '#';
next.tot++;
q.push(next);
} } } }
return -1;
}
int main()
{
while (~scanf(" %d %d", &m, &n))
{
for (int i = 1; i <= m;i++)
for (int j = 1; j <= n; j++)
{
scanf(" %c", &mp[i][j]);
if (mp[i][j] == 'a')
{
start.x = i;
start.y = j;
start.tot = 0;
}
}
int ans = bfs();
if (ans+1)
{
printf("%d\n", ans);
}
else
printf("Poor ANGEL has to stay in the prison all his life.\n"); }
return 0;
}

Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)的更多相关文章

  1. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  2. Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  3. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  4. 搜索 基础 AC 2014-01-14 15:53 170人阅读 评论(0) 收藏

    题目网址:http://haut.openjudge.cn/xiyoulianxi1/1/ 1:晶矿的个数 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 ...

  5. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  6. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  7. save与Update的合并操作 标签: 关系映射 2017-07-13 15:11 7人阅读 评论(0) 收藏

    做save与update的方法合并操作时,判断条件是主体对象的ID是否存在. 但是当页面中,涉及到多个主体对象的关联对象时,情况变得复杂起来,特总结项目中的几点 一.页面中的VO对象属性可以分为三类: ...

  8. <a>标签中的href伪协议 标签: html 2016-12-24 22:38 365人阅读 评论(0)

    <a id="jsPswEdit" class="set-item" href="javascript:;">修改密码</ ...

  9. 获取元素属性中的[x] 标签: javascript 2016-12-24 22:35 105人阅读 评论(0)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. document.write : 什么是在html输出中使用,什么是文档加载后使用?

    理解:您只能在 HTML 输出中使用 document.write.如果您在文档加载后使用该方法,会覆盖整个文档. Javascript中只能在 HTML 输出流中使用 document.write, ...

  2. 关系测试# 或 print(s2-s)Python 集合

    1集合是一个无序的,不重复的数据组合,它的主要作用如下(set和dict类似,也是一组key的集合,但不存储value.由于key不能重复,所以,在set中,没有重复的key): 去重,把一个列表变成 ...

  3. oracle 直接复制表内容到新表

    不知道为什么,刚建的oracle数据库删除数据很慢,表里面有120多万数据,非常地慢 于是采用的复制的方法,命令如下: create table students_backup as select * ...

  4. libjpeg安装和使用

    转自: http://blog.csdn.net/ice__snow/article/details/52563944 ,有几处做了一部分修改 一. 编译 下载地址 http://www.ijg.or ...

  5. ZOJ2412 Farm Irrigation(农田灌溉) 搜索

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

  6. jzoj P1163 生日派对灯

    在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码.这些灯都连接到四个按钮:  按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是 ...

  7. Delphi中静态方法重载还是覆盖的讨论

    Delphi中静态方法重载还是覆盖的讨论 新人学习Delphi的时候,容易搞不懂的一个问题,当子类方法和基类方法同名,并且参数也一样的时候,叫做什么呢?是覆盖,还是重载呢? 答案是隐藏父类方法. 一般 ...

  8. Spring IOC(八)bean 的创建

    Spring IOC(八)bean 的创建 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 参考: 每天用心记录一点点.内 ...

  9. ssh密钥讲解

    我们用ssh连接机器时候需要输用户名.密码,但是直接写账户文件的时候由于用的是明文,就存在安全的问题了.别人一旦截取了数据就获得了隐私了.这时候就用上ssh的密钥. ssh的密钥存是成对出现的,一个叫 ...

  10. 通过SD卡来安装Linux系统

    一.制作SD启动卡(安装Linux)步骤: 烧写原理:superboot-6410.bin(bootloader)+内核镜像文件+根文件系统 1.将SD卡插入USB接口的读卡器,并插在PC的USB口 ...