概述
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27406 Accepted Submission(s): 9711
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
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
题意:
~
代码:
1 //一道很简单的题卡了很长时间。从重点出发找起点,优先队列存 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<functional> 8 using namespace std; 9 int n,m; 10 struct Lu 11 { 12 int x,y,cnt; 13 friend bool operator<(Lu a,Lu b) 14 { 15 return a.cnt>b.cnt; 16 } 17 }; 18 bool vis[205][205]; 19 int dir[4][2]={1,0,-1,0,0,1,0,-1}; 20 char ch[205][205]; 21 int bfs(int sx,int sy) 22 { 23 priority_queue<Lu>q; 24 Lu L1,L2; 25 L1.x=sx;L1.y=sy;L1.cnt=0; 26 q.push(L1); 27 vis[sx][sy]=1; 28 while(!q.empty()) 29 { 30 L2=q.top(); 31 q.pop(); 32 if(ch[L2.x][L2.y]=='r') 33 return L2.cnt; 34 for(int i=0;i<4;i++) 35 { 36 L1.x=L2.x+dir[i][0]; 37 L1.y=L2.y+dir[i][1]; 38 if(L1.x<0||L1.x>=n||L1.y<0||L1.y>=m) continue; 39 if(vis[L1.x][L1.y]) continue; 40 if(ch[L1.x][L1.y]=='#') continue; 41 if(ch[L1.x][L1.y]=='x') 42 L1.cnt=L2.cnt+2; 43 else L1.cnt=L2.cnt+1; 44 vis[L1.x][L1.y]=1; 45 q.push(L1); 46 } 47 } 48 return -1; 49 } 50 int main() 51 { 52 int sx,sy; 53 while(scanf("%d%d",&n,&m)!=EOF) 54 { 55 for(int i=0;i<n;i++) 56 { 57 scanf("%s",ch[i]); 58 for(int j=0;j<m;j++) 59 { 60 if(ch[i][j]=='a') 61 { 62 sx=i;sy=j; 63 } 64 } 65 } 66 memset(vis,0,sizeof(vis)); 67 int ans=bfs(sx,sy); 68 if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.n"); 69 else printf("%dn",ans); 70 } 71 return 0; 72 }
转载于:https://www.cnblogs.com/--ZHIYUAN/p/5994949.html
最后
以上就是野性羊为你收集整理的HDU1242 BFS+优先队列Rescue的全部内容,希望文章能够帮你解决HDU1242 BFS+优先队列Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复