HDU 1242 Rescue (BFS+优先队列)
来源:互联网 发布:淘宝商品降价通知 编辑:程序博客网 时间:2024/05/22 15:35
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27264 Accepted Submission(s): 9664
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
题意:要找到能从r到a的最短路,其中#代表障碍不能通行,.代表可以走的路且花费1unit时间,x代表警卫,杀死一个警卫耗费1unit时间,问到a所需的最短时间。
思路:题意很明确,一眼也看出用bfs做,比较可能有问题的地方就是在于杀死警卫会耗费1unit时间,所以会影响bfs,所以用优先队列实现,每次都pop出来所花费时间最少的,也就是如果有一条路需要杀死警卫,有一条路不需要杀死警卫,那就先pop出不需要杀死警卫的那条路。还有一个巧妙的地方在于起点有很多个,但终点只有1个,所以可以从a找r,只要找到一个r即可返回节省时间。
#include <iostream>#include <string.h>#include <algorithm>#include <math.h>#include <queue>using namespace std;#define INF 999999char c[205][205];int book[205][205];int n,m;int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};struct node{ int x; int y; int step; friend bool operator < (node a, node b) { return a.step > b.step; }};priority_queue<node> q;int bfs(){ while(!q.empty()){ node t=q.top(); //cout<<t.step<<endl; if(c[t.x][t.y]=='r'){ return t.step; } q.pop(); for(int i=0;i<4;i++){ int dx=t.x+dir[i][0]; int dy=t.y+dir[i][1]; if(dx<0||dy<0||dx>=n||dy>=m||book[dx][dy]==1||c[dx][dy]=='#') continue; book[dx][dy]=1; node ss; ss.x=dx; ss.y=dy; ss.step=t.step+1; if(c[dx][dy]=='x'){ ss.step+=1; q.push(ss); } q.push(ss); } } return -1;}int main(){ while(scanf("%d %d",&n,&m)!=EOF){ memset(book,0,sizeof(book)); while(!q.empty()) q.pop(); node t; for(int i=0;i<n;i++){ scanf("%s",&c[i]); for(int j=0;j<m;j++){ if(c[i][j]=='a'){ t.x=i; t.y=j; t.step=0; book[i][j]=1; q.push(t); } } } int k=bfs(); if(k==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",k); } return -1;}
0 0
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- HDU-1242-Rescue(优先队列+BFS)
- HDU 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(bfs+优先队列)
- HDU:1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+【优先队列】)
- HDU 1242 Rescue(BFS+优先队列)
- 【HDU】-1242-Rescue(BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- linux配置tomcat
- Spring MVC 实现跨域资源 CORS 请求
- 【Java多线程与并发库】5.线程范围内共享变量的概念与作用
- Matlab Robotic Toolbox V9.10工具箱(四):常用函数
- JavaScript中的表格对象
- HDU 1242 Rescue (BFS+优先队列)
- Haar特征检测 - 人脸识别
- STL: for_each
- 前端模板的原理与实现
- 拯救白领颈椎:治疗肩周
- Spring 支持 CORS 请求踩的坑
- 关于Servlet的过滤器
- mysql 分组和获取随机记录
- Spring 使用 Quartz 任务调度定时器