4.2.1 Rescue

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

思路:BFS,看了下别人的代码,学了学队列,发现还挺有用的,但个人不喜欢不断更新最小值那种,所以我是每一刻都保证是最小(一个守卫用flag保证它走两次)

  1 #include <cstdio>
  2 #include <cmath>
  3 #include <queue>
  4 #include <iostream>
  5 #include <cstdlib>
  6 using namespace std;
  7 
  8 struct qq
  9 {
 10 int x,y;
 11 bool f;
 12 };
 13 
 14 queue<qq> q;
 15 qq ya,e;
 16 const int maxn=210;
 17 const int dx[]={1,-1,0,0};
 18 const int dy[]={0,0,-1,1};
 19 char map[maxn][maxn];
 20 int ax,ay,len=0,n,m,jud,step;
 21 bool vis[maxn][maxn];
 22 
 23 void close()
 24 {
 25 //fclose(stdin);
 26 //fclose(stdout);
 27 exit(0);
 28 }
 29 
 30 int judge()
 31 {
 32     if (map[ya.x][ya.y]=='r') return 3;
 33     if (ya.x>=0 && ya.y>=0 && vis[ya.x][ya.y]==false)
 34     {
 35         if (map[ya.x][ya.y]=='.') return 1; //无压力
 36         if (map[ya.x][ya.y]=='x') return 2; //有守卫
 37     } 
 38     else return 12345;
 39 return 123123;
 40 }
 41 
 42 void bfs()
 43 {
 44 memset(vis,false,sizeof(vis));
 45   while (!q.empty())
 46       q.pop();
 47   qq s;
 48   s.x=ax; s.y=ay; s.f=false;
 49   q.push(s);
 50   step=0;
 51   while ( (!q.empty()))
 52     {
 53         step++;
 54         //printf("----------STEP!!!STEP:::%d------------\n",step);
 55         len=q.size();
 56     //    printf("len:%d\n",len);
 57         for (int o=0;o<len;o++)
 58         {
 59             e=q.front();    //取出队列的第一个元素; 
 60             q.pop();
 61         //    printf("e.x:%d e.y:%d e.f:%d\n",e.x,e.y,e.f);
 62             if (e.f==true) // 就是说上一次就是守卫,所以这一次重新入队
 63             {
 64                 e.f=false;
 65                 q.push(e);
 66                 continue;
 67             }
 68           for (int i=0;i<4;i++)
 69              {
 70                  ya.x=e.x+dx[i];
 71                  ya.y=e.y+dy[i];
 72                  jud=judge();
 73             //    printf("ya.x:%d ya.y:%d jud!!:%d\n",ya.x,ya.y,jud);
 74                if (jud==1)  //就是平地,没有任何神马神马神马啊
 75                 {
 76                     ya.f=false;
 77                     vis[ya.x][ya.y]=true;
 78                     q.push(ya);
 79                     continue;
 80                 }
 81                 if (jud==2)  //就是有守卫,必须要干掉!
 82                 {
 83                     ya.f=true;
 84                     vis[ya.x][ya.y]=true;
 85                     q.push(ya);
 86                     continue;
 87                 }
 88                 if (jud==3)
 89                 {
 90             //        printf("STEP!!!!!!!!!!%d\n",step);
 91                printf("%d\n",step);
 92                     return ;
 93                 }
 94             }
 95         }
 96     }
 97 printf("Poor ANGEL has to stay in the prison all his life.\n");
 98 }
 99 
100 
101 void init()
102 {
103 //freopen("rescue.in","r",stdin);
104 //freopen("rescue.out","w",stdout);
105 while (scanf("%d%d",&n,&m)!=EOF)
106 {
107     memset(map,'\0',sizeof(map));
108     for (int i=0;i<n;i++)
109     {
110         scanf("%s",map[i]);
111         for (int j=0;j<m;j++)
112             if (map[i][j]=='a')
113             {
114                 ax=i;
115                 ay=j;
116             }
117     }
118     bfs();
119 }
120 }
121 
122 
123 int main ()
124 {
125 init();
126 //bfs();
127 close();
128 return 0;
129 }

PS:第一次写博文啊,写得很丑请见谅~。

原文地址:https://www.cnblogs.com/cssystem/p/2825768.html