HDU 1242 Rescue(优先队列)
题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=1242
题目描述:
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.)
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.
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."
#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(优先队列)的更多相关文章
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
- HDU 1242——Rescue(优先队列)
题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...
- HDU 1242 Rescue (BFS+优先队列)
题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...
随机推荐
- date 命令详解
date - print or set the system date and time Display the current time in the given FORMAT, or set th ...
- JavaScript及jQuery中的各种宽高属性图解
文/poetries(简书作者)原文链接:http://www.jianshu.com/p/60332df38393 著作权归作者所有,转载请联系作者获得授权, 并标注“简书作者”. 作者声明:本 ...
- Unix 文件系统读写时权限校验
文件系统中的所有文件都是在读出或写入时进行权限校验 一个问题,如果一个用户对一个普通文件有读写权限,在使用vim编辑时,管理员撤销掉此用户对此文件的写入权限 那么,这个普通用户还可以将修改写入文件吗?
- AJAX结合PHP整理复习
Ajax主要的功能是实现了浏览器端 异步 访问服务器:通过浏览器的XMLHttpRequest对象发出小部分数据,与服务端进行交互,服务端返回小部分数据,然后更新客户端的部分页面. 下图是一次请求成功 ...
- c#代码技巧
1.#region #endregion 1.#region 是一个分块预处理命令,主要用于编辑代码分段,在编译时会自动屏蔽,同时该指令可以使代码在VS代码编辑器中折叠或展开: 2.#region必须 ...
- CDN 边缘规则,三秒部署、支持定制、即时生效,多种规则覆盖常用业务场景
2017年的最后一周,又拍云进行了一次重要升级,将自定义 Rewrite 升级为"边缘规则".互联网应用场景的日益多样化,简单.方便.快速的根据不同应用场景实现不同的功能变得越来越 ...
- Java学习笔记8---类的静态成员变量与静态成员方法的访问与调用方式
(注:静态变量修改为静态成员变量,静态方法改为静态成员方法) 静态成员变量又称类变量,静态成员方法又称类方法,它们统称为静态成员或类成员.静态成员由static修饰,是属于整个类的,所有的对象共享这些 ...
- Linux下用ls和du命令查看文件以及文件夹大小
ls的用法 ls -l |grep "^-"|wc -l或find ./company -type f | wc -l 查看某文件夹下文件的个数,包括子文件夹里的. ls -lR ...
- 用mint ui去实现滚动选择日期并可以关闭拾取器
转发要备注出处哈,么么哒 注释的那些部分都是我在尝试的时候写得,留给自己看得,删除不影响效果哈,希望对你们有帮助,比较忙可能写得很粗糙,不好意思,有空再改了 实例一: <template&g ...
- 如何在 iOS 真机运行 Appium
使用 gui 启动的 appium 1.2.2 也会有这个问题,所以你要把 deviceconsole 复制到 /Applications/Appium.app/Contents/Resources/ ...