HDOJ 1242 Rescue(BFS + 优先队列)
来源:互联网 发布:ubuntu jdk1.7 安装 编辑:程序博客网 时间:2024/05/09 05:24
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
解题思路:一直以为是一道简单的BFS,所以就用队列做的,结果WA了9次,和别人讨论之后才知道要用优先队列做。由于天使只有一个而朋友可能有很多,所以可以从天使开始BFS离他最近的朋友,如果可以到达朋友所在的位置就打印输出时间,否则输出Poor ANGEL has to stay in the prison all his life.
代码如下:
#include <cstdio>#include <queue>#include <cstring>#include <algorithm>using namespace std;const int maxn = 205;const int maxm = 205;typedef struct Node{ int x; int y; int t;}node;bool operator < (node a,node b){ return a.t > b.t;}char maze[maxn][maxm];int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};int ax,ay,rx,ry;int m,n;void bfs(){ priority_queue<node> que; //queue<node> que; node temp,a; a.x = ax; a.y = ay; a.t = 0; maze[a.x][a.y] = '#'; que.push(a); while(que.size()){ node p = que.top(); que.pop(); maze[p.x][p.y] = '#'; for(int i = 0;i < 4;i++){ int nx = p.x + dir[i][0]; int ny = p.y + dir[i][1]; if(0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] != '#'){ if(maze[nx][ny] == 'r'){ printf("%d\n",p.t + 1); return ; } else if(maze[nx][ny] == 'x'){ temp.t = p.t + 2; } else if(maze[nx][ny] == '.'){ temp.t = p.t + 1; } temp.x = nx; temp.y = ny; que.push(temp); } } } printf("Poor ANGEL has to stay in the prison all his life.\n");}int main(){ while(scanf("%d %d",&n,&m) != EOF){ for(int i = 0;i < n;i++){ scanf("%s",maze[i]); } for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ if(maze[i][j] == 'a'){ ax = i; ay = j; break; } } } bfs(); } return 0;}/*在网上找了一份代码,运行下面的数据得到的结果不对,交到OJ上却AC了,奇怪6 8#########.xxxx.##......##ax....rrrrrrrrrrrrrrrrr*/
0 0
- hdoj 1242 Rescue (bfs+优先队列)
- HDOJ 1242 Rescue(BFS + 优先队列)
- HDOJ 1242 Rescue (BFS+优先队列)
- HDOJ 1242 Rescue 优先队列+bfs
- hdoj 1242 Rescue 【BFS + 优先队列】
- HDOJ 1242 Rescue(bfs+优先队列)
- HDOJ-1242(Rescue)(bfs+优先队列)
- hdoj 1242 Rescue (bfs 优先队列)
- HDOJ 1242 Rescue(优先队列+BFS)
- HDOJ 1242 Rescue (BFS + 优先队列)
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- 第十二章 内存控制块
- 程序员的成长与规划 | 送签名书啦 | StuQ专访foruok
- for 循环 嵌套练习
- 中通服的第26天
- 剑指offer 66题 【回溯法】矩阵中的路径
- HDOJ 1242 Rescue(BFS + 优先队列)
- 用C++函数指针封装自定义的Button
- Apache 安装出现 Failed to open the Windows service manager, perhaps you forgot to log in as Adminstrator
- 编程中的抽象
- Spring依赖注入:注解注入总结
- python 学习(九) 之 continue语句
- java分页技术核心代码实现
- 一道位运算的题目,一串数字,别的都出现了3次只有一个出现2次,要你找到他
- H5媒体查询