hdu 1242 Rescue
2015-06-28 13:08
501 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
bfs+优先队列。。
bfs+优先队列。。
[code]#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<map> using std::map; using std::cin; using std::cout; using std::endl; using std::find; using std::sort; using std::pair; using std::vector; using std::priority_queue; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) decltype((c).begin()) #define cls(arr,val) memset(arr,val,sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) const int Max_N =210; typedef unsigned long long ull; const int dx[] = { 0, 0, 1, -1 }, dy[] = { 1, -1, 0, 0 }; bool vis[Max_N][Max_N]; char maze[Max_N][Max_N]; int N, M, Sx, Sy, Dx, Dy; struct Node { int x, y, s; Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {} bool operator<(const Node &a) const { return s > a.s; } }; void bfs() { cls(vis, false); priority_queue<Node> que; que.push(Node(Sx, Sy, 0)); vis[Sx][Sy] = true; while (!que.empty()) { Node tp = que.top(); que.pop(); if (tp.x == Dx && tp.y == Dy) { printf("%d\n", tp.s); return; } rep(i, 4) { int nx = dx[i] + tp.x, ny = dy[i] + tp.y; if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue; if (maze[nx][ny] == '#' || vis[nx][ny]) continue; if (maze[nx][ny] == 'x') que.push(Node(nx, ny, tp.s + 2)); else que.push(Node(nx, ny, tp.s + 1)); vis[nx][ny] = true; } } puts("Poor ANGEL has to stay in the prison all his life."); } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif while (~scanf("%d %d", &N, &M)) { rep(i, N) { scanf("%s", maze[i]); rep(j, M) { if (maze[i][j] == 'a') Dx = i, Dy = j; else if (maze[i][j] == 'r') Sx = i, Sy = j; } } bfs(); } return 0; }
相关文章推荐
- ArrayBlockingQueue源码剖析
- VirtualBox下快速创建新的虚拟机(一)——复制vdi文件
- Word Frequency
- APUE 头文件apue.h 解决方法
- Java常见的queue(队列)类测试
- 【Arduino学习笔记】蓝牙控制手自两用避障车
- 在jfinal中使用druid,并配置查看权限
- LeetCode_60---Permutation Sequence
- 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化
- [安卓]手机管家(二十一)杀毒UI及SlidingDrawer(抽屉效果)
- UIAlertView自动弹框创建步骤
- UI控件设置圆角
- UITextView弹簧效果属性
- 创建UIImageView方式的区别
- [iOS8][Segue转场]转场返回无法绑定的解决办法
- 【Leetcode】Combination Sum III #216
- Google interview question: quickSort-like questions
- Google interview question: mergeSort-like questions
- Arduino MPU6050学习资料总结
- 探究requestDisallowInterceptTouchEvent失效的原因