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求最短路问题,直接一码,样例一过,一交wa,再交wa,不对劲,怎么回事。
  • 原来题目说的是Angel’s friends 朋友们!!!,更可恶的是样例还只给出了一个朋友,这就导致不停的哇哇哇
  • 其次这个题目好在哪里呢,反向遍历!!!,因为我们并不知道朋友的数量如果每个点都要去枚举的话,可能会超时,所以这时候我们就需要用到剪枝操作,我们直接从终点开始遍历,只要遇到朋友的位置就直接返回,这样就会快的很多,这里我们用一个优先队列去操作一番,每次都取出最小的,看看能不能满足要求,一旦满足要求就直接return
  • 可是!!!,一旦满足上述所有的要求,跨过所有的坑,我,今天,还是,wa wa wa了!!!究竟是为什么,直到现在我还是没想明白,我想只要bfs返回的值大于零那么就找到了一个可行解,但是还是没过题,题目一定要返回值不等-1才让输出就对了,可我真的想不通,或许我再想想就明白了呢,但是这个题目真的wa了超级多次,绝对是一个好题!!!

AC代码

#include <bits/stdc++.h>
using namespace std;
int n, m, vis[1000][1000];
int d[4][2] = {
   1, 0, -1, 0, 0, 1, 0, -1}, sx, sy, flag;
char c[1000][1000];
struct node
{
   
   int x, y, step;
   bool operator<(const node x) const
   {
   
      return step > x.step;
   }
};
int bfs()
{
   
   memset(vis, 0, sizeof(vis));
   priority_queue<node> q;
   node s, e;
   s.x = sx;
   s.y = sy;
   s.step = 0;
   q.push(s);
   while (!q.empty())
   {
   
      s = q.top();
      q.pop();
      if (c[s.x][s.y] == 'r')
         return s.step;
      for (int i = 0; i < 4; ++i)
      {
   
         e.x = s.x + d[i][0];
         e.y = s.y + d[i][1];
         if (e.x >= 1 && e.x <= n && e.y >= 1 && e.y <= m && c[e.x][e.y] != '#' && !vis[e.x][e.y])
         {
   
            vis[e.x][e.y] = 1;
            if (c[e.x][e.y] == 'x')
               e.step = s.step + 2;
            else
               e.step = s.step + 1;
            q.push(e);
         }
      }
   }
   return -1;
}
int main()
{
   
   while (~scanf("%d%d", &n, &m))
   {
   
      flag = 0;
      for (int i = 1; i <= n; ++i)
         for (int j = 1; j <= m; ++j)
         {
   
            scanf(" %c", &c[i][j]);
            if (c[i][j] == 'a')
            {
   
               sx = i;
               sy = j;
            }
         }
      if (bfs() == -1)
         puts("Poor ANGEL has to stay in the prison all his life.");
      else
         printf("%d\n", bfs());
   }
   return 0;
}
不是所有的努力都会成功,不是所有的汗水都有回报。然而,这又如何?哪怕很多事情都会事与愿违,可我努力过,便已问心无愧