之前用dfs剪枝AC了,http://www.cnblogs.com/ediszhao/p/4741825.html,这次用bfs+priority_queue来尝试解题

题意:拯救行动,天使r有多个朋友a(friends,在这里被坑了几次,没看清题意),天使被关在牢房里,等着朋友来拯救,求拯救天使的最短距离。

以天使为起点进行bfs,找到的a就是最小拯救时间值。

#include <iostream>
#include <cstring>
#include <queue> using namespace std; struct node
{
int x,y,cnt;
friend bool operator < (node a,node b)
{
return a.cnt > b.cnt;
}
};
const int M = ;
char map[M][M];
int visited[M][M];
int n,m; int dire[][] = {{,},{,},{-,},{,-}};
priority_queue <struct node> q;
int bfs()
{
node now,next;
while (!q.empty())
{
now = q.top();
q.pop();
for (int i = ; i< ; i++)
{
int x = now.x+dire[i][];
int y = now.y+dire[i][];
if (x >= && x < n && y >= && y < m && map[x][y]!='#' && visited[x][y] == )
{
next.x = x;
next.y = y;
if (map[x][y] == 'a')
{
return (now.cnt+);
}
if (map[x][y] == 'x')
{
next.cnt = now.cnt+;
}
else next.cnt = now.cnt+;
visited[x][y] = ;
q.push(next);
}
}
}
return ;
}
int main()
{
while (cin >> n >> m)
{
node nn;
while (!q.empty())    //一定要清空之前的队列,在这里wrong了
q.pop();
for (int i =; i < n; i++)
{
for (int j = ; j < m; j++)
{
cin >> map[i][j];
visited[i][j] = ;
if (map[i][j] == 'r')
{
nn.x = i;
nn.y = j;
nn.cnt = ;
q.push(nn);
visited[i][j] = ;
}
}
}
int res = bfs();
if (!res)
cout << "Poor ANGEL has to stay in the prison all his life.\n";
else cout << res << endl;
}
return ;
} /*
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
2 8
a.#####r
#..xxaxx
*/

hdoj1242(bfs+priority_queue)的更多相关文章

  1. hdu 1175 bfs+priority_queue

    连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS ( ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. hdu1242 优先队列+bfs

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

  4. POJ2049Finding Nemo(bfs + 构图)

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8456   Accepted: 1975 Desc ...

  5. HDU 2209 翻纸牌游戏 状态BFS

    翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem De ...

  6. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  7. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

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

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

  9. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

随机推荐

  1. 二分查找 - vb.net

    Module Module1    Sub Main()        Dim array(999) As Integer        Dim searchValue As Integer      ...

  2. 001.net开发环境与变量

    1 .net 技术平台,用来创建互联互通的应用程序 2 应用 嵌入式开发企业级应用 3 .NET Framework 技术:公共语言运行时 .NET Framework的工作模式:.NetC#> ...

  3. 这个发现是否会是RSA算法的BUG、或者可能存在的破解方式?

    笔者从事各种数据加解密算法相关的工作若干年,今天要说的是基于大数分解难题的RSA算法,可能有些啰嗦. 事情的起因是这样的,我最近针对一款芯片进行RSA CRT解密的性能优化.因为期望值是1024bit ...

  4. CodeForces 456A

    Laptops Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. 转载:MyEclipse安装插件的几种方法

    地址:http://www.cnblogs.com/pharen/archive/2012/02/08/2343342.html 本文讲解MyEclipse(MyEclipse10)的三种方法,以SV ...

  6. ArcGIS Pro 简明教程(3)数据编辑

    ArcGIS Pro 简明教程(3)数据编辑 by 李远祥 数据编辑是GIS中最常用的功能之一,ArcGIS Pro在GIS数据编辑上使用习惯有一定的改变,因此,本章可以重点看看一些编辑工具的使用和使 ...

  7. GIS制图课程目录

    由于更新次序跳跃式更新,因此很有必要整理一下全书目录,并将会按照实际学习的顺序进行更新. [前言] GIS制图课程前言 [理论篇] 理论篇-地图学与GIS制图的基础理论(一) 理论篇-地图学与GIS制 ...

  8. 时间戳 获得当前时间 -iOS

    //获取当前时间戳 static inline NSString * getTimeStamp(){ NSTimeInterval d=[[NSDate date] timeIntervalSince ...

  9. IBM面试记

    link:http://kb.cnblogs.com/page/107213/ 话说其实我很久没有被正经面试过了.一开始去微软实习自然经过了经典的笔试和几轮面试,然后去了朋友的创业公司并立即被激动集团 ...

  10. Markdown 标记语言简介

    简介 做为一种标记语言,广泛应用在写作领域,markdown语法编写的文本,经过特殊的软件进行展示: 目的:相对html,markdown提高文本的可读可写性 兼容:兼容html 自动转换特殊字符:& ...