题目链接

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

分析:

bfs即可,可能有多个’r’(天使的朋友),而’a’(天使)只有一个,从’a’开始搜,找到的第一个’r’即为所求

需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就并不能保证每次出队的是时间最小的元素,所以要用优先队列。

代码:

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int n,m,sx,sy;
int flg[4][2]= {{-1,0},{1,0},{0,1},{0,-1} };
char Map[209][209];
int vis[209][209];
struct Node
{
int x,y;
int step;
friend bool operator<(const Node &a,const Node &b)
{
return a.step>b.step;
}
};
int bfs(int x,int y)
{
Node Now,Next;
Now.x=x;
Now.y=y;
Now.step=0;
priority_queue<Node> q;
q.push(Now);
while(!q.empty())
{
Now=q.top();
q.pop();
//printf("%d %d\n",Now.x,Now.y);
if(Map[Now.x][Now.y]=='r')
{
// printf("@@@@@@@\n");
return Now.step;
} for(int i=0; i<4; i++)
{
Next.x=Now.x+flg[i][0];
Next.y=Now.y+flg[i][1];
if(Next.x>=0&&Next.x<n&&Next.y>=0&&Next.y<m&&Map[Next.x][Next.y]!='#'&&vis[Next.x][Next.y]==0)
{
vis[Next.x][Next.y]=1;
if(Map[Next.x][Next.y]=='x')
Next.step=Now.step+2;
else
Next.step=Now.step+1;
q.push(Next);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
scanf(" %s",Map[i]);
int flag=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(Map[i][j]=='a')
{
sx=i;
sy=j;
flag=1;
// printf("%d %d\n",sx,sy);
break;
}
}
if(flag==1)
break;
}
vis[sx][sy]=1;
int ans=bfs(sx,sy);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
}

HDU 1242 Rescue (广搜)的更多相关文章

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

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

  2. hdu 1242 Rescue

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

  3. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

  4. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

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

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

  6. hdu 1242 Rescue(bfs)

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

  7. HDU 1242 Rescue(优先队列)

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

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

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

  9. HDU 1242 rescue (优先队列模板题)

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

随机推荐

  1. 开发 | 如何在微信小程序的页面间传递数据?

    我们在之前发布过小程序页面传值方法的简单介绍,说明了在小程序开发中,两种常见的页面之间传值方法. 本期,知晓程序(微信号 zxcx0101)为你带来的是「倒数记日」小程序开发者带来的,小程序开发中,有 ...

  2. laravel多环境配置(local,testing,production)

    根据不同的环境有不同的配置,laravel中,可以把配置写到.env文件中 在系统中,可以使用env(key, "默认值")来获取env中的配置信息 在laravel中运行时,会运 ...

  3. java 实用类

                                                java  实用类 1.File类为管理文件和目录提供了方法,其对象表示一个文件或者目录.它提供了若干方法对文件 ...

  4. 018 final 关键字的用途

    final关键字的含义 final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量.一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变量再次初 ...

  5. Post Lamps CodeForces - 990E(暴力出奇迹?)

    题意: 在一个从0开始的连续区间上  放置几个小区间,使得这些小区间覆盖整个大区间,不同长度的小区间有不同的花费,其中有m个点,小区间的左端点不能放在这些点上 解析: 显然如果0是这m点中的一个 则无 ...

  6. 【刷题】BZOJ 2243 [SDOI2011]染色

    Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如 ...

  7. 【BZOJ5334】数学计算(线段树)

    [BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...

  8. 【转】高手带你深入理解ucos任务堆栈

    首先,我们来理解一下两个概念: 1.堆栈就是一段连续的空间.用于存储数据的,在c计算机中有很多应用,比如发生中断时保存现场,c语言函数调用时保存现场和临时变量. 2.堆栈指针就是一个数据指针.有时候计 ...

  9. 【django基础之ORM,增删改查】

    一.定义 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候, ...

  10. 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?

    在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...