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. 1.Spring Cloud初相识--------简单项目搭建

    开发工具:STS 代码下载链接:GitHub管理项目 前言: Springcloud 算是当前比较火的技术,一套微服务架构的技术. 我个人对微服务的理解为: 服务可以代表service,微服务就是小的 ...

  2. 2.初识CronTrigger

    开发工具:Eclipse 代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git 前言: 在1.初始Quartz里面,我们介绍了quartz的一 ...

  3. 5、SpringBoot+Mybatis整合------多对多

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...

  4. 基于SOA架构和流媒体技术的在线教育平台的研究

    简介 现代远程教育是指通过音频.视频(直播或录像)以及包括实时和非实时在内的计算机技术把课程传送的教育.现代远程教育是随着现代信息技术的发展而产生的一种新型教育方式.计算机技术.多媒体技术.通信技术的 ...

  5. Redis高可用

    redis高可用只要在于三个方面 主从复制 哨兵机制 集群机制 主从复制 主从复制作用: 1.数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式.2.故障恢复:当主节点出现问题时,可 ...

  6. IDEA的下载安装和激活

    1.下载网站http://www.jetbrains.com/ 2. 3. 4.注意要下载Ultimate版本 5.安装 下载完成后直接下一步,傻瓜式安装 6.激活,在图片位置输入激活码即可 !!!! ...

  7. laravel-多条件查询并指定key输出

    $room = DB::table('room') ->where(function($query) use($contList){ foreach ($contList as $k=>$ ...

  8. JavaSE 第二次学习随笔(作业一)

    package homework2; import java.io.ObjectInputStream.GetField; import java.util.Arrays; public class ...

  9. 学习python第十四天,模块

    Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...

  10. 4、spring boot 配置文件之profile

    Profile  1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml 默认使用application.prop ...