题目

/******************以下思路来自百度菜鸟的程序人生*********************/

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

  需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就

并不能保证每次出队的是时间最小的元素,所以要用优先队列,第一次用优先队列,还不熟练哇

  优先队列(priority_queue)的基本操作:

  empty(); 队列为空返回1

  pop();   出队

  push();  入队

  top();   返回队列中优先级最高的元素

  size();  返回队列中元素的个数

/**************************************************************/

修改了学妹的代码,增加了优先队列,成功AC~

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[][];
bool visit[][];
int xx[]={,-,,};
int yy[]={,,,-};
int n,m,x,y;
struct node
{
int x,y;
int time;
friend bool operator < (const node &a,const node &b)
{
return a.time>b.time;
}
};
void bfs(int &min)
{
priority_queue<node>q;
int i;
node step,temp;
step.x=x;
step.y=y;
step.time=;
visit[x][y]=true;
q.push(step);
while(!q.empty())
{
step=q.top();
q.pop();
if(map[step.x][step.y]=='r')
{
min=step.time;
return ;
}
else if(map[step.x][step.y]!='r')
{
for(i=;i<;i++)
{ if(map[step.x+xx[i]][step.y+yy[i]]!='#' && !visit[step.x+xx[i]][step.y+yy[i]])
{
if(map[step.x+xx[i]][step.y+yy[i]]=='x')
{
temp.x=step.x+xx[i];
temp.y=step.y+yy[i]; temp.time=step.time+;
}
else
{
temp.x=step.x+xx[i];
temp.y=step.y+yy[i];
temp.time=step.time+;
}
visit[temp.x][temp.y]=true;
q.push(temp);
}
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int i,j;
int min=;
memset(visit,false,sizeof(visit));
for(i=;i<=n+;++i)
map[i][]=map[i][m+]='#';
for(i=;i<=m+;++i)
map[][i]=map[n+][i]='#';
for(i=;i<=n;++i)
{
for(j=;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
x=i;
y=j;
}
}
}
bfs(min);
if(min<)
printf("%d\n",min);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return ;
}

hdu 1242 Rescue(BFS,优先队列,基础)的更多相关文章

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

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

  2. HDU 1242 Rescue(优先队列)

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

  3. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  4. hdu 1242 Rescue (BFS)

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

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

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

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

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

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

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

  8. hdu 1242 Rescue

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

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

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

随机推荐

  1. 杭电ACM2091--空心三角形

    http://acm.hdu.edu.cn/showproblem.php?pid=2091 scanf 和 printf的输入输出功能并不强大.有时候我们需要清空输入输出流,所以一定切记getcha ...

  2. userscript.user.js 文件头

    // ==UserScript== // @name MyUserScript // @namespace https://github.com/MrLeo // @description users ...

  3. 下载服务器dll文件并动态加载

    1.新加一个类库 namespace ClassLibrary1 { public class Class1 { public int Add(int a, int b) { return a + b ...

  4. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...

  5. Android Error:You must supply a layout_width attribute……

    出现这种情况的可能原因目前本人碰到的有: 1:在xml文件中某个属性名或者属性值写错,请务必仔细检查你有没有写错某个拼写. 2:当你在,比如TextView中,没有声明layout_width,经测试 ...

  6. 用Python作GIS之一:介入STARS

    STARS的全称是Space-Time Analysis of Regional Systems,直译过来就是区域系统时空分析软件.这是针对区域多时相数据的分析包,源代码公开.该软件将最近几年发展起来 ...

  7. GIS论文翻译问题

    1 在sci库中输入关键词,搜索一篇相关的英文。看看专业词汇怎么翻译。做个记录 2打开ArcGIS中文online和英文online帮助文档。在中文帮助中搜索中文。找到相应的位置,再切换到英文的版本中 ...

  8. ios开发之NavBar和TarBar使用技巧

    1  改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2  隐藏“back”按钮: self.navigationItem.hidesBackButton = YE ...

  9. (C学习基础)一,CMD的使用

    以后考研要考数据结构,所以系统的整理一下C学习笔记.这里开发用VS2013  CMD资料在这里下载 首先就是DOS系统的CMD的使用,是不是觉得会命令行语句的人特别牛呢,有时候使用命令行总是事半功倍, ...

  10. hadoop相关问题

    发现一篇不错的文章,转一下.http://www.cnblogs.com/xuekyo/p/3386610.html HDFS导论(转)   1.流式数据访问 HDFS的构建思想是这样的:一次写入,多 ...