题目来源:

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. pstree 命令详解

    作用: 以命令树状图的方式展现进程之间的派生关系, 显示效果比较直观. 选项: -a 显示每个程序的完整指令, 包含路径, 参数或者是常驻服务的标志 -c 不使用精简标示法 -h 列出树状图,特别标明 ...

  2. java 分解质因数

    算法目的:对一个正整数分解质因数 一.算法分析: 1.建立整数列表,保存求到的因数. 2.声明整数i=2,用以递增取模:整数m,用于临时保存n 3.建立while循环,i小于等于整数m时,判断m%i, ...

  3. RSA加解密实现

    RSA是由MIT的三位数学家R.L.Rivest,A.Shamir和L.Adleman[Rivest等1978, 1979]提出的一种用数论构造双钥的方法,被称为MIT体制,后来被广泛称之为RSA体制 ...

  4. K:java中枚举的常见用法

    用法一:常量   在JDK1.5 之前,我们定义常量都是: public static fianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. ...

  5. Head First设计模式之目录

    只有沉淀.积累,才能远航:沉沉浮浮,脚踏实地. 这本书已经闲置了好久,心血来潮,决定写个目录,让自己坚持看完这本书 创建型模式 抽象工厂模式(Abstract factory pattern): 提供 ...

  6. 关于SPF记录的说明

    详细了解SPF记录的可以查看一下这篇博文:http://www.renfei.org/blog/introduction-to-spf.html 关于 SPF SPF:Sender Policy Fr ...

  7. Redis锁构造

    单线程与隔离性 Redis是使用单线程的方式来执行事务的,事务以串行的方式运行,也就是说Redis中单个命令的执行和事务的执行都是线程安全的,不会相互影响,具有隔离性. 在多线程编程中,对于共享资源的 ...

  8. android利用adb安装应用程序出现“more than one device and emulator wait for device ”

    今天,写自动化脚本时,因在这之前进行了一下真机版本的更新,还没有从电脑上拔出,就又在adt打开了一个AVD. 于是,自由自在的就去写脚本了,企图在emulator上装一个APK,在cmd窗口下,利用: ...

  9. [转载]解决sudo: sorry, you must have a tty to run sudo

    前几天遇到一个问题,在一个终端中调用另一个shell,始终是无法执行的,后来捕捉到报错信息为sudo: sorry, you must have a tty to run sudo,后来,在网上了解到 ...

  10. python函数高级特性

    掌握了Python的数据类型.语句.函数,基本可以编写出很多有用的程序了.但是Python中,代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好.基于这一思想,我们来介绍python中 ...