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. c#devexpres TreeList 最简单显示动态值的应用

    为了让数据显示在行内,也为熟练一下devexpress treelist  控件, 查找了很多,最多的是先把数据放在datatable  表里边, 然后赋值给treelist的datasource 的 ...

  2. Oracle_PL/SQL(5) 包

    包1.定义:包用于逻辑组合相关的PL/SQL类型,项和子程序,由包规范和包体组成 建立包规范:包规范是包与应用程序之间的接口,用于定义包的公用组件, 包括常量,变量,游标,过程,函数等 建立包体:用于 ...

  3. andorid 菜单 进度条

    activity_ui2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  4. linux基础编程:IO模型:阻塞/非阻塞/IO复用 同步/异步 Select/Epoll/AIO(转载)

      IO概念 Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file ...

  5. javascript数组中数字和非数字下标的区别(转)

    http://blog.csdn.net/qq_27461663/article/details/52014911 考完试后闲来无事,想起好多天没写js了,于是打算实践一下最近看到的一些好玩的点子.结 ...

  6. html 转化成 pdf

  7. Telnet远程登录

    假设 电脑A Telnet远程登录 电脑B (Windows) 1.电脑B: 关闭防火墙 开启Telnet服务:“我的电脑”-->“管理”-->“服务”-->Telnet开启 2.电 ...

  8. 在linux虚拟机上安装Docker

    1.简介Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像: 运行中的这个 ...

  9. [RF]怎样用Robot Framework写好Test Case?

    1.介绍 这是一个关于如何用Robot Framework写好Test Case的高层次的指导准则 怎样实际的与系统进行交互不在此文档范围内 最重要的准则是使测试用例尽可能的让熟悉此领域的人觉得简单易 ...

  10. 用php脚本比较MySQL两个数据库的结构差异

    define('DATABASE1', 'mysql://root:password@127.0.0.1/db1'); $dbi1 = new DbMysql; $dbi1->dbh = DAT ...