HDU 1242 Rescue
来源:互联网 发布:淘宝详情图怎么上传 编辑:程序博客网 时间:2024/06/08 01:23
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
题意:这个犯人在a,朋友在r,x是狱官,朋友要进去救犯人,走一格1时间,如果遇到狱官就杀掉,杀人需要1时间,求朋友走到a最少要多久。如果走不到,就输出那一段话。
优先队列解这道题,感觉不用优先队列也不会超时,毕竟值最大的点只有2,但我还是用优先队列,主要熟悉这种算法,毕竟这道没什么特殊要求,很典型。
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;struct node{ int i,j,t; friend bool operator<(node a,node b) { return a.t>b.t; }};char c[205][205];int a[205][205];int b[4][2]= {1,0,0,1,-1,0,0,-1};int main(){ int a1,a2,r1,r2; int i,j,m,n; int map[205][205]; while(~scanf("%d%d",&m,&n)) { memset(a,0,sizeof(a)); memset(map,0,sizeof(map)); getchar(); for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { scanf("%c",&c[i][j]); if(c[i][j]=='.') a[i][j]=1; if(c[i][j]=='x') a[i][j]=2; if(c[i][j]=='a') { a1=i; a2=j; a[i][j]=1; } if(c[i][j]=='r') { r1=i; r2=j; a[i][j]=1; } } getchar(); } node front,rear; priority_queue<node>q; front.i=r1; front.j=r2; front.t=0; q.push(front); int e=0; while(!q.empty()) { front=q.top(); q.pop(); if(front.i==a1&&front.j==a2) { e=1; printf("%d\n",front.t); break; } for(i=0; i<4; i++) { rear.i=front.i+b[i][0]; rear.j=front.j+b[i][1]; if(rear.i>0&&rear.j>0&&rear.i<=m&&rear.j<=n&&a[rear.i][rear.j]) { if(front.t+a[rear.i][rear.j]<map[rear.i][rear.j]||map[rear.i][rear.j]==0) { rear.t=front.t+a[rear.i][rear.j]; map[rear.i][rear.j]=rear.t; q.push(rear); } } } } if(!e) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
1 0
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- SURF学习(一)——积分图
- C#动态编译类
- grep设置查找颜色高亮
- UI_UIImage
- Android插件化原理系列(一)启动未声明的Activity
- HDU 1242 Rescue
- Android studio 上使用aidl总结
- 【笔记】NodeMcu做为Client与Server通信(未整理)
- python 字符串
- nodeJs个人笔记
- CurrentHashMap putIfAbsent(key,value)用法讨论
- UI_UIControl
- SEO企业文案思路:给的再多,不如懂我
- BaseAdapter使用