hdoj1242(bfs+priority_queue)
之前用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)的更多相关文章
- hdu 1175 bfs+priority_queue
连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS ( ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu1242 优先队列+bfs
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ2049Finding Nemo(bfs + 构图)
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8456 Accepted: 1975 Desc ...
- HDU 2209 翻纸牌游戏 状态BFS
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem De ...
- 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 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- MyBatis 插入时返回自增主键
XML <insert id="insert" parameterType="com.stone.model.Person"> <!-- se ...
- HDU5879(打表)
Cure Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HNOI2002(伸展树)
营业额统计 Time Limit:5000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status ...
- RedisRepository分享和纠错
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- Condition源码分析
转:http://www.nbtarena.com/Html/soft/201308/2429.html Condition的概念 大体实现流程 I.初始化状态 II.await()*作 III.si ...
- CF 752C. Santa Claus and Robot
C. Santa Claus and Robot time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- iOS自定义视图- SJTextView
需求: textView 需要placeholder用来提示输入 textView 要做字数限制 textView 禁止表情符号的输入 思考: 因为需求比较通用,便想通过自定义SJTextView来实 ...
- js原生轮播图
轮播图是新手学前端的必经之路! 直接上代码! <!DOCTYPE html><html lang="en"><head> <meta ch ...
- 第七届蓝桥杯javaB组真题解析-凑算式(第三题)
题目 /* 凑算式 B DEF A + --- + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字. 比 ...
- java_web总结(一)
1.struts1ajax返回值 public ActionForward preChangeAccountPwd(ActionMapping mapping, ActionForm form, Ht ...