杭电 1242 Rescue (BFS+优先队列)

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;
}
不是所有的努力都会成功,不是所有的汗水都有回报。然而,这又如何?哪怕很多事情都会事与愿违,可我努力过,便已问心无愧
全部评论

相关推荐

就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
06-20 19:40
中原工学院 Java
网络存储:十几天不会让你拉人办卡就结束了吧?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务