Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12927    Accepted Submission(s): 4733

Problem 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
 
Author
CHEN, Xue
 
Source
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1072 1175 1026 1180 1401 
 
 //31MS    356K    1410 B    C++
/* 题意:
从点a到点r,求最短步数 BFS:
典型的BFS,这题数据有点怪.最好用优先队列实现bfs。 */
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int mov[][]={,,,,-,,,-};
char map[][];
int bx,by,n,m;
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
void bfs(int x,int y)
{
priority_queue <node> Q;
node t={x,y,};
Q.push(t);
while(!Q.empty())
{
t=Q.top();
Q.pop();
for(int i=;i<;i++)
{
node tt=t;
tt.x+=mov[i][];
tt.y+=mov[i][];
tt.step++;
if(tt.x>=&&tt.x<n&&tt.y>=&&tt.y<m&&map[tt.x][tt.y]!='#')
{
if(map[tt.x][tt.y]=='r')
{
cout<<tt.step<<endl;
return;
}
else if(map[tt.x][tt.y]=='x')
tt.step++;
map[tt.x][tt.y]='#';
Q.push(tt);
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life.\n";
}
int main(void)
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
bx=i,by=j;
}
bfs(bx,by);
}
return ;
}

hdu 1242 Rescue (BFS)的更多相关文章

  1. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  2. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  3. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  4. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  5. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  6. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  7. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  8. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

随机推荐

  1. Objective-c 单例设计模式

    Objective-c 单例设计模式 一.什么是单例模式:(Singleton)      单例模式的意图是是的类的对象成为系统中唯一的实例,提供一个访问点,供客户类共享资源.   二.什么情况下使用 ...

  2. CocoaAsyncSocket使用

    代理的.h文件 #import <Foundation/Foundation.h> #import "GCDAsyncSocket.h" typedef void(^S ...

  3. 利用python在windows环境下爬取赶集网工作信息。

    主要用到了多进程和多线程的知识,最后结果保存成csv文件格式,如有需要可改成数据库版本. 对用到的库做下简要介绍,具体请参考官方文档: xpinyin.Pinyin:将输入的中文转成拼音 concur ...

  4. PLC状态机编程第五篇-状态机自动生成PLC程序

    这篇比较简单了,我就直接上图了,不多废话. 一.选择求解器,一定要选择定步长的. 二.右击Chart状态机,出现图上菜单 三.左边红色的勾选择,选择右侧的菜单,然后点击Generate Code按钮, ...

  5. R-biomaRt使用-代码备份

    目标:使用R脚本从ensembl上下载transcript数据 简单粗暴,直接上代码.biomaRt的介绍晚一点更新. # this file helps extract information fr ...

  6. python系列4之装饰器

    目录 递归算法解析 冒泡排序解析 装饰器解析 一. 递归 1. 递归的定义 递归(Recursion),又成为递回,在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.递归一词还较长用于描述以 ...

  7. liteos学习文档liteos.github.io

    https://liteos.github.io该主页是华为liteos物联网操作系统的文档,里面有一章是“内核指南”,讲的是rtos的最主要的功能.可以当作liteos的入门了解,如果用rtos的使 ...

  8. 浅谈XX系统跨平台迁移(测试环境)

    一 概述 XX系统目前运行在XX-A的云平台上,计划将其迁移至XX-B的云平台. XX系统是java开发,中间组件涉及nginx+keepalived实现各个业务系统之间的高可用,kafka,zook ...

  9. Postgres主备切换

    主备查询 主备不会自动切换(即需要实现线上环境主数据库宕掉之后,从数据库能够自动切换为主数据库,需要借用第三方软件,例如heartbeat等) (1)如何查看是primary还是standby 方法1 ...

  10. 17-比赛1 C - Binary Nim (栈的游戏)

    题目描述 Tweedle-Dee 和 Tweedle-Dum 正在进行一场激烈的二进制 Nim 游戏.这是你没有玩过的船新版本,游戏包含 N 个栈,每个栈只包含 0 和 1 的元素.就像一般的 Nim ...