Rescue
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,X是守卫,移动到.需要一步,X两步。#是墙。求r找到a的最少步数;
注意:r可能不止一个。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 int n,m,ans; 8 char a[222][222]; 9 bool b[222][222]; 10 int nx[4]={-1,1,0,0}; 11 int ny[4]={0,0,-1,1}; 12 struct node{ 13 int x; 14 int y; 15 int step; 16 }; 17 int BFS(int x,int y) 18 { 19 node s; 20 s.x=x; 21 s.y=y; 22 s.step=0; 23 b[x][y]=true; 24 queue<node>q; 25 q.push(s); 26 while(!q.empty()) 27 { 28 node now=q.front(); 29 q.pop(); 30 if(a[now.x][now.y]=='r') 31 return now.step; 32 for(int i = 0;i < 4;i++) 33 { 34 node end; 35 end.x=now.x+nx[i]; 36 end.y=now.y+ny[i]; 37 end.step=now.step; 38 if(b[end.x][end.y]==false&&(a[end.x][end.y]=='.'||a[end.x][end.y]=='r')) 39 { 40 b[end.x][end.y]=true; 41 end.step+=1; 42 q.push(end); 43 } 44 else if(b[end.x][end.y]==false&&a[end.x][end.y]=='x') 45 { 46 b[end.x][end.y]=true; 47 end.step+=2; 48 q.push(end); 49 } 50 } 51 } 52 return -1; 53 54 } 55 int main() 56 { 57 while(scanf("%d %d",&n,&m)!=EOF) 58 { 59 int i,j; 60 memset(b,false,sizeof(b)); 61 for(i = 0;i < n;i++) 62 scanf("%s",a[i]); 63 for(i = 0;i < n;i++) 64 for(j = 0;j < m;j++) 65 { 66 if(a[i][j]=='a')/*让a找r,因为r可能不止一个,而a只有一个*/ 67 { 68 ans=BFS(i,j); 69 break; 70 } 71 } 72 if(ans == -1) 73 printf("Poor ANGEL has to stay in the prison all his life.\n"); 74 else 75 printf("%d\n",ans); 76 77 78 } 79 return 0; 80 }

浙公网安备 33010602011771号