hdu 1242 Rescue(优先队列)
来源:互联网 发布:淘宝买家怎么导出订单 编辑:程序博客网 时间:2024/05/21 17:46
http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <stack>#include <queue>using namespace std;#define N 310#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;//const int INF = ((1<<31)-1);const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};char str[N][N];int k, n, m, vis[N][N];struct node{ int x, y, t; friend bool operator < (const node &a, const node &b) { return a.t > b.t;//时间短的先出队 }}sa;int BFS (){ priority_queue <node> que;//优先队列 que.push (sa); met (vis, 0); vis[sa.x][sa.y] = 1; while (que.size()) { node Sa = que.top(); que.pop(); if (str[Sa.x][Sa.y] == 'r') return Sa.t; for (int i=0; i<4; i++) { node q = Sa; q.x += dir[i][0], q.y += dir[i][1]; if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && !vis[q.x][q.y] && str[q.x][q.y]!='#') { q.t++; vis[q.x][q.y] = 1; if (str[q.x][q.y] == 'x') q.t++; que.push (q); } } } return -1;}int main (){ while (scanf ("%d %d", &n, &m) != EOF) { k = 0; for (int i=0; i<n; i++) { getchar (); for (int j=0; j<m; j++) { scanf ("%c", &str[i][j]); if (str[i][j] == 'a') sa = (node) {i, j, 0}; } } int ans = BFS (); if (ans == -1) puts ("Poor ANGEL has to stay in the prison all his life."); else printf ("%d\n", ans); } return 0;}
0 0
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- hdu 1242 Rescue (优先队列)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- [ACM] HDU 1242 Rescue (优先队列)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- [ACM] HDU 1242 Rescue (优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- hdu 1242 Rescue(优先队列)
- HDU-1242-Rescue(优先队列+BFS)
- HDU 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(bfs+优先队列)
- HDU:1242 Rescue(BFS+优先队列)
- grep 命令系列:如何在 Linux/UNIX 中使用 grep 命令
- tomcat配置文件server.xml详解
- structs2 请求json数据
- C++——数据结构之 顺序队列 链式队列?
- Android studio 安装篇
- hdu 1242 Rescue(优先队列)
- Miller_Rabin · 大数
- 双向一对多外联关系-XML配置属性
- 欢迎使用CSDN-markdown编辑器
- android开发中观察者模式的实际应用
- 彻底解决Spring MVC 中文乱码 问题
- Scala trait 未完待续
- sublime 修改字体
- 栈与队列(下)