HDOJ 1242 Rescue(bfs+优先队列)
来源:互联网 发布:一个月工资3000 知乎 编辑:程序博客网 时间:2024/05/21 15:42
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21127 Accepted Submission(s): 7549
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
题意:Angel被抓起来关在了监狱里,给出监狱地图,#表示围墙,.表示道路,a是Angel的位置,r是她朋友的位置,x是监狱守卫的位置(碰到监狱守卫是必须杀死他,花费时间一秒)。在监狱中只能上下左右前进,每前进一格花费一秒。问是否能从r到达a,若能,输出最小时间。
题解:将点的坐标和从r到此处的步数存入一个结构体中,按照步数少的优先排序。 每次遍历优先队列中的首元素,即可找到到达r的最少步数。
代码如下:
<span style="font-size:12px;">#include<cstdio>#include<queue>using namespace std;int n,m,x,y,ex,ey,ans;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};char map[205][205];struct node{int x,y,step;}a,temp;bool operator < (const node &x,const node &y){return x.step>y.step;}int ok(node temp){if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&map[temp.x][temp.y]!='#') return 1;return 0;}int bfs(){int i;a.x=x;a.y=y;a.step=0;priority_queue<node>q;q.push(a);while(!q.empty()){a=q.top();q.pop();if(a.x==ex&&a.y==ey) return a.step;for(i=0;i<4;i++){temp.x=a.x+dir[i][0];temp.y=a.y+dir[i][1];if(ok(temp)){if(map[temp.x][temp.y]=='.'||map[temp.x][temp.y]=='a') temp.step=a.step+1;else temp.step=a.step+2;map[temp.x][temp.y]='#';q.push(temp);}}}return -1;}int main(){int i,j;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++){if(map[i][j]=='r'){x=i;y=j;}if(map[i][j]=='a'){ex=i;ey=j;}}}ans=bfs();if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0;}</span>
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 +优先队列)
- hdu 1242 Rescue(bfs+优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue 优先队列+BFS
- HDU 1242 Rescue(BFS,优先队列)
- 【iOS学习笔记】生成带文本的UIImage
- 河内之塔(汉诺塔)
- 霧島レイNavigates 沢城みゆきのRadio Drive 泽城美雪
- 实战篇:finereport如何搭建机电行业综合管理平台
- HTML5 Web 存储功能
- HDOJ 1242 Rescue(bfs+优先队列)
- dbus1.0.2、libxm2交叉编译、安装
- Maven插件开发
- filter的配置和filter模板
- kafka测试时异常
- Use '$' instead of '.' for inner classes (or use only lowercase letters in package names) 问题
- protocol - 1
- poj1276 多重背包问题 转换为经典的01背包
- java获取文件创建时间