题目来源:

http://acm.hdu.edu.cn/showproblem.php?pid=1242

题目描述:

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
 
题意描述:
有多个‘r’和一个‘a’,问从r走向a最短时间是多少,其中遇到x表示守卫,需要多加一个单位时间。
解题思路:
刚上来以为是最短路,用了BFS来做,后来发现,BFS找到的是最短步数,与最短时间是不一样的,也就是说,最短步数的时间不一定是最短时间。最后使用优先队列,出队的时候保证该点的时间时最短的即可。
代码实现:
 #include<stdio.h>
#include<queue>
using namespace std; int r,c;
struct point {
int x,y,s;
bool operator < (const point &a) const
{
return a.s<s;
}
}; char map[][];
int bfs(int sx,int sy); int main()
{
int i,j,sx,sy;
while(scanf("%d%d",&r,&c) != EOF)
{
for(i=;i<=r;i++)
for(j=;j<=c;j++)
{
scanf(" %c",&map[i][j]);
if(map[i][j]=='a')
{
sx=i;sy=j;
}
} int ans=bfs(sx,sy); if(!ans)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return ;
}
int bfs(int sx,int sy)
{
int next[][]={,,,,,-,-,};
priority_queue<struct point>q;
struct point cur,nex;
int k,tx,ty; cur.x=sx;
cur.y=sy;
cur.s=;
q.push(cur);
map[sx][sy]='#'; while(!q.empty())
{
cur=q.top();
for(k=;k<=;k++)
{
tx=cur.x+next[k][];
ty=cur.y+next[k][];
if(tx < || tx > r || ty < || ty > c)
continue;
if(map[tx][ty] != '#')
{
nex.x=tx;
nex.y=ty;
if(map[tx][ty]=='x')
nex.s=cur.s+;
if(map[tx][ty]=='.')
nex.s=cur.s+;
if(map[tx][ty]=='r')
return cur.s+;
q.push(nex);
map[tx][ty]='#';
}
}
q.pop();
}
return ;
}

易错分析:

优先队列还是直接使用C++的模板好,自己写的容易出问题

 
 
 

HDU 1242 Rescue(优先队列)的更多相关文章

  1. hdu 1242 Rescue

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

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

    题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...

随机推荐

  1. Docker(六):Docker网络配置进阶

    1.Docker集群网络配置之Weave Weave是Github上一个比较热门的Docker容器网络方案,具有非常良好的易用性且功能强大.仓库地址:https://github.com/weavew ...

  2. 字符串MD5加密运算

    public static string GetMd5String(string str)       {           MD5 md5 = MD5.Create();           by ...

  3. 未来五年什么样的IT技术最具颠覆性?这里有你想知道的答案

    据外媒报道称,近日Gartner研讨会在美国弗罗里达州奥兰多举行,智能化.大数据和物联网成为届研讨会的三大主题.市场研究机构Gartner Research的副总裁兼资深研究员大卫·卡利(David ...

  4. ASP.NET Core MVC中的 [Required]与[BindRequired]

    在开发ASP.NET Core MVC应用程序时,需要对控制器中的模型校验数据有效性,元数据注释(Data Annotations)是一个完美的解决方案. 元数据注释最典型例子是确保API的调用者提供 ...

  5. Spark 核心概念 RDD 详解

    RDD全称叫做弹性分布式数据集(Resilient Distributed Datasets),它是一种分布式的内存抽象,表示一个只读的记录分区的集合,它只能通过其他RDD转换而创建,为此,RDD支持 ...

  6. 阿里云的oss使用技巧

    1初始化: 使用阿里云sdk包(php) 方法一:使用composer 加载sdk包 composer require aliyuncs/oss-sdk-php 或 "require&quo ...

  7. Python+selenium+eclipse+pydev自动化测试环境搭建

    一.        安装python 1.下载安装python 可访问python的官方网站:http://www.Python.prg找到下载页面下载需要的版本,可下载python2.x或者pyth ...

  8. Struts2内部执行过程

    首先是Struts2的流程图. 一.当有一个请求的时候.执行以下流程. 1 客户端初始化一个指向Servlet容器的请求: 2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做Act ...

  9. Android 获取唯一标识替代方法

    private static String getTheOnlyID() { String onlyOne; //获取IMEI TelephonyManager TelephonyMgr = (Tel ...

  10. 机器学习小记——KNN(K近邻) ^_^ (一)

    为了让绝大多数人都可以看懂,所以我就用简单的话语来讲解机器学习每一个算法 第一次写ML的博文,所以可能会有些地方出错,欢迎各位大佬提出意见或错误 祝大家开心进步每一天- 博文代码全部为python 简 ...