之前用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. jQuery 判断checkbox是否被选中 4种方法

    下午写JS验证,有一个需求需要判断 checkbox是否被选择,查阅相关资料后,总结以下4种方法,分享给大家. <!DOCTYPE html> <html lang="en ...

  2. Linux笔记(十一) - 文件系统管理

    (1)文件系统查看命令:df [选项] [挂载点]-a 显示所有文件系统信息,包括特殊文件系统,如/proc /sysfs-h 使用习惯单位显示容量,如KB,MB或GB-T 显示文件系统类型-m 以M ...

  3. 史上最全的synchronized解释

    首先:推荐使用synchronized(obj)这种方法体的使用方式,一个类里面建议尽量使用单一的同步方法,多种方法混用,维护成本太大. 其次:关于java5.0新增的ReenTrantLock方法: ...

  4. MEAN教程1-MongoDB安装和使用

    MEAN是MongoDB.Express.AngularJS和Node.js的缩写.其理念是仅使用JavaScript一种语言来驱动整个应用.其最鲜明的特点有以下几个:1整个应用只使用一种语言:2整个 ...

  5. GitHub客户端发布托管代码

    初试GitHub及客户端使用 突然想分享代码,于是记起来曾几何时有人提到过GitHub这个东西,于是便各种百度,注册申请了一个账号,下载了windows客户端,全英文网站就连新手教程也是全英的,现在想 ...

  6. 在ubuntu linux 中编写一个自己的python脚本

    在ubuntu linux 中编写一个自己的简单的bash脚本. 实现功能:终端中输入简单的命令(以pmpy为例(play music python),为了区别之前说的bash脚本添加了py后缀),来 ...

  7. We Chall-Prime Factory-Writeup

    MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...

  8. Codeforce 712A Memory and Crow

    A. Memory and Crow time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  9. 【noip 2009】 乌龟棋 记忆化搜索&动规

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  10. java基础知识点---equal,==,hashcode

    1.==比较对象之间的地址是否相同 student a=new student(1); student b=new student(1); a==b   false b=a; a==b   true ...