HDU 1242 Rescue (广搜 + 优先队列)
2016-03-30 21:28
218 查看
HDU 1242
学会了优先队列的使用,就是要把用时最少的方案放在队列最前面。
关于对 < 的重载不是很懂,好像只要a > b 就是把小的放在队前,即最小堆,感觉好像和sort相反。。
参考博客:/article/8151429.html
学会了优先队列的使用,就是要把用时最少的方案放在队列最前面。
关于对 < 的重载不是很懂,好像只要a > b 就是把小的放在队前,即最小堆,感觉好像和sort相反。。
参考博客:/article/8151429.html
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; int N, M; const int inf = 0x3f3f3f3f; int map[210][210], vis[210][210]; int sx, sy; int ans; struct node { int x, y, time; friend bool operator < (node a, node b) { return a.time > b.time; } }; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; void bfs() { priority_queue<node> q; node now, next; now.time = 0, now.x = sx, now.y = sy; q.push(now); vis[sx][sy] = 1; while(!q.empty()) { now = q.top(); q.pop(); if(map[now.x][now.y] == 'r') { ans = now.time; return; } int i; for(i = 0; i < 4; i++) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.time = now.time; if(next.x >= 0 && next.x < N && next.y >= 0 && next.y < M && map[next.x][next.y] != '#' && vis[next.x][next.y] == 0) { vis[next.x][next.y] = 1; if(map[next.x][next.y] == 'x') next.time += 2; else next.time += 1; q.push(next); } } } } int main() { while(~scanf("%d %d", &N, &M)) { getchar(); int i, j; for(i = 0; i < N; i++) { for(j = 0; j < M; j++) { scanf("%c", &map[i][j]); if(map[i][j] == 'a') { sx = i, sy = j; } } getchar(); } memset(vis, 0, sizeof(vis)); ans = inf; bfs(); if(ans == inf) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n", ans); } return 0; }
相关文章推荐
- EasyUI的load,reload,loadData
- 毫秒级和纳秒级计时的API--timeGetTime、GetTickCount、QueryPerformanceCounter
- Android UI杂谈---layout_weight
- disque概要
- error:This Android SDK requires Andr...ate ADT to the latest version.
- 内核编译时生成uImage的办法
- iOS学习之UI初级————UIView的概念属性以及相关方法
- kernel编译生成Image zImage uImage的区别
- System.Web.UI.Page事件执行顺序
- Funambol Developer's Guide 中 connector development样例的问题
- UITabEx
- iOS开发之UITabBarController的创建和使用
- 【JAVA】 UIMnager
- UVA 11404 Palindromic Subsequence 【LCS】
- poj--3061--Subsequence(技巧)
- easyui-datagrid 编辑单元格
- CF_5C_LongestRegularBracketSequence
- UIRichLabel
- windowbuilder入门
- IOS开发-UI学习-UITextField的各种属性设置