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. BZOJ 3007 [SDOI2012]拯救小云公主 - 对偶图 + 并查集

    Solution 答案具有单调性, 显然可以二分答案. 有两个注意点 : 英雄是可以随便走的, 也就是不是网格图... 还有坐标不能小于$1$ QAQ 开始时英雄在左下角, 公主在右上角, 我们反过来 ...

  2. 一种基于URL数据源的WEB报表插件

    完全支持所见所得的报表设计, 支持 PHP ,Java 等所有支持JSON格式的后端. 立即下载测试版本 需要正式版本?请QQ联系:1565498246 或者留言

  3. 厉害了,他用PS不是P照片而是……

    今儿要介绍的主角是战斗民族的设计师 Dmitriy Glazyrin,他这个人用PS做设计有个特点,专门P3D软件做出来的白模. 大家可以想象一下,一个什么颜色什么材质都没有的东西,把它楞是用PS加上 ...

  4. Tomcat的下载、安装、启动与关闭

    ubuntu server 16.04 从官网下载 Binary Distributions 版本的相应的压缩包, https://tomcat.apache.org/download-90.cgi ...

  5. spring security 4.2后出现CouldnotverifytheprovidedCSRFtokenbecauseyoursessionwasnotfound

    升级到spring security 4.2后,登录不了,出现下面的错误 WARN DefaultHandlerExceptionResolver:361 - Failed to bind reque ...

  6. Bootstrap学习遇到的role属性--- 无障碍网页应用属性

    以前接触过Bootstrap,但也只是仅仅接触,现在重新学习下,今天看到一个例子中的属性有一个role, 查阅资料发现这个是--WAI-ARIA无障碍设计属性: 通俗说是该设计为了一些盲人,失聪,残疾 ...

  7. 销售vs技术岗,做技术的方法思考

    销售甚至比技术岗位挣得还多,当然,做技术的比较好的拿到的自然也多. 我在想个问题,技术的天然优势是可以不断地积累,包括写code,写博客,做流程,完善流程,自动化流程,或者把某些工作流程化,自动化,托 ...

  8. java script入门之知识

    1.注释 /*              */ 多行 //单行 2.常见形式 <!DOCTYPE html><html><head><title>My ...

  9. 7月底的list

    多校的新姿势: 超大数比较 置换群 树归 莫比乌斯反演 7月26日做了的list: a.补了多校的两道题. b.学了如何比较特别多特别大的数 c.看了波循环群   d.看了点kmp 7月27想做的li ...

  10. kbmmw 中XML 操作入门

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...