ZOJ 1649 BFS
2016-05-12 21:48
459 查看
#include <cstdio> #include <queue> #include <cstring> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 2E2 + 10; const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, { -1, 0}}; struct Node { int x, y; int step; bool operator<(const Node &oth)const { return step > oth.step; } }; Node cur, Next; char mp[maxn][maxn]; int n, m, Arrival[maxn][maxn], Startx, Starty; int BFS() { priority_queue<Node>Q; cur.x = Startx; cur.y = Starty; cur.step = 0; Arrival[Startx][Starty] = 0; Q.push(cur); while (!Q.empty()) { cur = Q.top(); Q.pop(); if (mp[cur.x][cur.y] == 'a') return cur.step; for (int i = 0; i < 4; i++) { Next.x = cur.x + dir[i][0]; Next.y = cur.y + dir[i][1]; Next.step = cur.step + 1; if (mp[Next.x][Next.y] == 'x') Next.step++; if (mp[Next.x][Next.y] != '#' && Next.step < Arrival[Next.x][Next.y]) { Arrival[Next.x][Next.y] = Next.step; Q.push(Next); } } } return 0; } int main(int argc, char const *argv[]) { while (~scanf("%d%d", &n, &m)) { for (int i = 0; i <= m + 1; i++) mp[0][i] = mp[n + 1][i] = '#'; for (int i = 1; i <= n; i++) { scanf("%s", mp[i] + 1); mp[i][0] = mp[i][m + 1] = '#'; for (int j = 1; j <= m; j++) if (mp[i][j] == 'r') Startx = i, Starty = j; } memset(Arrival, INF, sizeof(Arrival)); int ans = BFS(); if (ans) printf("%d\n", ans); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; }
这题简直有鬼。。。
相关文章推荐
- Java锁机制
- 【css】所有的a标签设置为新窗口打开【原创】
- hdu 5256 序列变换(最长上升子序列&&二分)
- 操作系统中的页面置换算法
- 实现文本的复制和粘贴、文本的撤销和恢复(Swing JTextArea)
- Linux线程中pthread_join和pthread_detach函数
- git的学习笔记(冲突解决)
- 个人报告10
- 实验三 进程调度模拟程序
- fork wait
- 推荐算法之潜在因子(Latent Factor)算法
- poj 1080
- 【hdu4283】区间动态规划问题
- Ubuntu 14.04 LTS下为终端设置代理
- scroller与scrollto
- Renesas-入门
- struts配置
- PB中的Describe,Evaluatate,LookUpDisplay小结
- 空格替换
- Qt 打包应用程序方法