概述
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是守卫的位置,如果朋友们在营救过程中碰到了守卫,需要花一单位时间去打败守卫,并且每走一步也要花去一单位时间,求成功营救angel所花费的最少时间思路:用bfs和优先队列.代码如下:#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; char map[220][220]; int n,m,flag,sx,sy,ex,ey; int dir[4][2]={0,1,1,0,0,-1,-1,0}; struct node{ int x,y,step; friend bool operator < (node a,node b) { return a.step>b.step; } }; int judge(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() { node s; s.x=sx,s.y=sy,s.step=0; priority_queue<node>que; que.push(s); while(!que.empty()) { node now=que.top(); que.pop(); if(now.x==ex&&now.y==ey) { return now.step; } for(int l=0;l<4;l++) { node end; end.x=now.x+dir[l][0]; end.y=now.y+dir[l][1]; if(judge(end)) { if(map[end.x][end.y]=='.'||map[end.x][end.y]=='a') end.step=now.step+1; else end.step=now.step+2; map[end.x][end.y]='#'; que.push(end); } } } return -1; } int main() { int i,j; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; flag=0; for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(map[i][j]=='r') { sx=i;sy=j; } if(map[i][j]=='a') { ex=i,ey=j; } } } int ans=bfs(); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.n"); else printf("%dn",ans); } }
最后
以上就是俊秀学姐为你收集整理的hdojRescue (BFS)的全部内容,希望文章能够帮你解决hdojRescue (BFS)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复