首页 > 代码库 > Rescue

Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16718    Accepted Submission(s): 6069


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.)
 

 

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.
 

 

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...
..#..#.#
#...##..
.#......
........
 
  1 /*  2     Name: Rescue  3     Copyright: Shangli_Cloud  4     Author: Shangli_Cloud  5     Date: 24/10/14 22:44  6     Description:   7 */  8 #include<iostream>  9 #include<cstdio> 10 #include<cstring> 11 #include<algorithm> 12 #include<cmath> 13 #include<set> 14 //#include<map> 15 #include<queue> 16 #include<stack> 17 #include<vector> 18 using namespace std; 19 const int ms=201; 20 const int inf=0x7fffffff; 21 int dir[4][2]={0,1,1,0,0,-1,-1,0}; 22 int min_time[ms][ms]; 23 char map[ms][ms]; 24 struct point 25 { 26     int x,y,time,step; 27 }; 28 queue<point> que; 29 int n,m; 30 int ax,ay,sx,sy; 31 void input() 32 { 33     //scanf("%d%d",&n,&m); 34     getchar(); 35     for(int i=0;i<n;i++) 36     { 37         for(int j=0;j<m;j++) 38         { 39             min_time[i][j]=inf; 40             scanf("%c",&map[i][j]); 41             if(map[i][j]==a) 42             { 43                 ax=i; 44                 ay=j; 45             } 46             else if(map[i][j]==r) 47             { 48                 sx=i; 49                 sy=j; 50             } 51         } 52         getchar(); 53     } 54     min_time[sx][sy]=0; 55     return ; 56 } 57 int dfs(point p) 58 { 59     point pp; 60     que.push(p); 61     while(!que.empty()) 62     { 63         pp=que.front(); 64         que.pop(); 65         for(int i=0;i<4;i++) 66         { 67             int x=pp.x+dir[i][0]; 68             int y=pp.y+dir[i][1]; 69             if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!=#) 70             { 71                 point t; 72                 t.x=x; 73                 t.y=y; 74                 t.step=pp.step+1; 75                 t.time=pp.time+1; 76                 if(map[x][y]==x) 77                     t.time++; 78                 if(t.time<min_time[x][y]) 79                 { 80                     min_time[x][y]=t.time; 81                     que.push(t); 82                 } 83             } 84         } 85     } 86     return min_time[ax][ay]; 87 } 88 int main() 89 { 90     int i,j; 91     while(scanf("%d%d",&n,&m)!=EOF) 92     { 93         input(); 94         point start; 95         start.x=sx; 96         start.y=sy; 97         start.time=0; 98         start.step=0; 99         int ans=dfs(start);100         if(ans<inf)101             printf("%d\n",ans);102         else103             printf("Poor ANGEL has to stay in the prison all his life.\n");104     }105     return 0;106 }

 

 

Sample Output
13

Rescue