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. ES6初识-模块化

    export let A=123; export function test(){ console.log('test'); } export class Hello(){ test(){ conso ...

  2. BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 989  Solved: 660[Submit][Status][Discuss] Description ...

  3. Mybatis与Hibernate区别

    Mybatis与Hibernate区别 mybatis: 1. 入门简单,即学即用,提供了数据库查询的自动对象绑定功能,而且延续了很好的SQL使用经验,对于没有那么高的对象模型要求的项目来说,相当完美 ...

  4. C#基础-面向对象-多态

    多态,不同对象对同一方法的不同实现 使用abstract关键字表示抽象类 // 表示是一个抽象类 public abstract class Animal { private string name; ...

  5. Salt-ssh 自动安装salt-minion

    作用:为了不手动去安装一台一台去salt-minion,并进重复的配置 一.环境 系统环境: #cat /etc/redhat-release CentOS Linux release 7.4.170 ...

  6. TFS 2015服务端安装与客户端签入项目步骤

    一.参考如下3篇文章搭建TFS2015环境 1.参考文章如下: TFS 2015(Visual Studio Team Foundation Server)的下载和安装http://www.cnblo ...

  7. RESTful API架构和oauth2.0认证机制(概念版)

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...

  8. MyCat实现数据库与数据库之间的读写分离

    一.Mycat的安装准备: 1.jdk:要求jdk必须是1.7及以上版本 2.Mysql:推荐mysql是5.5以上版本 3.Mycat: Mycat的官方网站: http://www.mycat.o ...

  9. 牛客暑假多校第二场J-farm

    一.题意 White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. T ...

  10. 十三、MySQL之IDE工具介绍及数据备份

    一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 二.MySQL数据备份 # ...