HDOJ-1242 Rescue -------BFS

Rescue

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


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

 

Sample Output
13
 
  1 /* 功能Function Description:   HDOJ 1242  
  2    开发环境Environment:        DEV C++ 4.9.9.1
  3    技术特点Technique:
  4    版本Version:
  5    作者Author:                 可笑痴狂
  6    日期Date:                   20120819
  7    备注Notes:
  8         不会优先队列,只能用数组排,还是经典的广搜题,BFS解决
  9     题目没说清,不过可以看出来题中输入数据的x代表守卫,#代表墙
 10 */
 11 #include<stdio.h>
 12 #include<stdlib.h>
 13 #define MAX 0x3fffffff
 14 
 15 int n,m,ans,endx,endy;
 16 char map[205][205];
 17 int dir[4][2]={-1,0,0,1,1,0,0,-1};
 18 typedef struct point
 19 {
 20     int x,y,time;
 21 }point;
 22 
 23 
 24 int cmp(const void *p,const void *q)
 25 {
 26     return ((point *)p)->time-((point *)q)->time;
 27 }
 28 
 29 int BFS(int i,int j)
 30 {
 31     point st,t;
 32     int k,front,tail;
 33     point q[40000];
 34 
 35     front=tail=0;
 36     st.x=j;
 37     st.y=i;
 38     st.time=0;
 39     q[tail++]=st;
 40     while(front!=tail)
 41     {
 42         st=q[front++];
 43         if(st.x==endx&&st.y==endy)
 44             return st.time;
 45         else
 46         {
 47             for(k=0;k<4;++k)
 48             {
 49                 t.x=st.x+dir[k][0];
 50                 t.y=st.y+dir[k][1];
 51                 if(map[t.y][t.x]=='#'||t.x<0||t.x>=m||t.y<0||t.y>=n)
 52                     continue;
 53                 else if(map[t.y][t.x]=='x')
 54                 {
 55                     t.time=st.time+2;
 56                     map[t.y][t.x]='#';
 57                 }
 58                 else
 59                 {
 60                     t.time=st.time+1;
 61                     map[t.y][t.x]='#';
 62                 }
 63                 q[tail++]=t;
 64             }
 65             qsort(&q[front],tail-front,sizeof(point),cmp);
 66         }
 67     }
 68     return MAX;
 69 }
 70 
 71 int main()
 72 {
 73     int i,j,tmp;
 74     while(scanf("%d%d",&n,&m)!=EOF)
 75     {
 76         ans=MAX;
 77         getchar();
 78         for(i=0;i<n;++i)
 79         {
 80             for(j=0;j<m;++j)
 81             {
 82                 scanf("%c",&map[i][j]);
 83                 if(map[i][j]=='a')
 84                 {
 85                     endy=i;
 86                     endx=j;
 87                 }
 88             }
 89             getchar();
 90         }
 91         for(i=0;i<n;++i)
 92             for(j=0;j<m;++j)
 93             {
 94                 if(map[i][j]=='r')
 95                 {
 96                     tmp=BFS(i,j);
 97                     if(tmp<ans)
 98                         ans=tmp;
 99                 }
100             }
101         if(ans==MAX)
102             printf("Poor ANGEL has to stay in the prison all his life.\n");
103         else
104             printf("%d\n",ans);
105     }
106     return 0;
107 }

 

你可能感兴趣的:(bfs)