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了吧,很明显是最短路径问题,不过又加了一个条 ...
随机推荐
- centOS7 mini配置linux服务器(四) 配置jdk
这里简单写一下centos7Mini 安装jdk1.8的全过程. 一.下载jdk,linux版本. 地址:http://www.oracle.com/technetwork/java/javase/ ...
- centos 系统日志
Linux系统日志主要有三类:连接时间日志.进程统计日志和错误日志 连接时间日志 连接时间日志由多个程序执行,把记录写入到/var/og/wtmp和/var/run/utmp.ogin等程序更新wtm ...
- 利用伪元素:after清除浮动
一.代码 html代码 <div class="clearfix"></div> css样式 .clearfix{ zoom:1;/*这个属性是为了兼容IE ...
- TP框架设置的LOG_LEVEL不起作用
最近监控系统日志,可是日志是全部级别的日志,没有办法看太多了.只想看有用的信息. 就在config文件中修改了配置文件.可是试了以后并没有变化,log文件还是全部级别的信息. 后来发现调试模式开启着, ...
- 深入研究ES6 Generators
ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 如果你还不知道 ...
- python matplotlib 绘图基础
在利用Python做数据分析时,探索数据以及结果展现上图表的应用是不可或缺的. 在Python中通常情况下都是用matplotlib模块进行图表制作. 先理下,matplotlib的结构原理: mat ...
- windows 配置接收报文是否中断
作用:网络编程的时候,编程接收报文,可以不用循环等待并判断是否报文接收完整.配置了windows禁用网络中端后,自己写的程序一次接收,便是整条报文. 步骤: 1."打开网络和共享中心&quo ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
- 把玩爬虫框架Gecco
如果你现在接到一个任务,获取某某行业下的分类. 作为一个非该领域专家,没有深厚的运营经验功底,要提供一套摆的上台面且让人信服的行业分类,恐怕不那么简单. 找不到专家没有关系,我们可以爬虫.把那些专家的 ...
- webpack编译流程漫谈
前言 weback在web构建工具的激烈竞争中逐渐脱引而出. 无论是编译速度.报错提示.可扩展性等都给前端开发者耳目一新的感觉.本篇文章是个人对webpack的一点小研究总结. webpack在开发者 ...