【原创】ZOJ_1649 Rescue 解题报告

Rescue

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

如题。简单的广搜水题,问题在于如何扩展x的结点。方法也很简单。可以把x看做一个有两个结点的连通分量,第一次进入x结点时,只能扩展出一个结点(还是x自己),第二次进入x结点时,才能扩展出周围四个方向的结点。

当然,为了避免出现死循环,必须要把x结点的值更改。我是更改为‘.‘。

最后的最小值,则是广搜树的高度。

下面是AC代码

#include <stdio.h>
#include <queue>
using namespace std;
/*
整整两个小时的时间!!!!
居然是忘记清空队列的低级BUG!!!!

1、标记是否访问过某个结点,如果访问过,则不再入队;
2、如果当前的结点值是x,则需要将这个访问过的结点再次入队,并更改为.,本轮不再扩展。类似于将该点展开为一个线段,头扩展的结点是尾,尾扩展周围的结点。

*/
typedef struct point
{
       int x;
       int y;
}point;

queue<point> road;
char prison[201][201];
int visit[201][201];
int high[201][201];
int n,m;
int startX,startY;

void init()
{
      for(int i = 0;i < n;i++)
     {
            for(int j = 0;j < m;j++)
           {
                     if(prison[i][j] == ‘a‘)
                    {
                         startX = i;
                         startY = j;
                    }
           }
      }
      for(int i = 0;i < 201;i++)
               for(int j = 0;j < 201;j++)
               {
                         high[i][j] = 0;
                         visit[i][j] = 0;
               }
       while(!road.empty())
                 road.pop();    //f*cking bug occurs here.
}

void BFS()
{
       point tmp;
       point p1;
       tmp.x = startX;
       tmp.y = startY;
       int finalX,finalY;
       high[startX][startY] = 0;
       visit[startX][startY] = 1;
       road.push(tmp);
       int flag = 0;
       while(!road.empty())
      {
               tmp = road.front();
               road.pop();
               if(prison[tmp.x][tmp.y] == ‘x‘)
               {
                     road.push(tmp);
                     high[tmp.x][tmp.y] ++;
                     prison[tmp.x][tmp.y] = ‘.‘;
               }
              else if(prison[tmp.x][tmp.y] == ‘r‘)
              {
                     flag = 1;
                     finalX = tmp.x;
                     finalY = tmp.y;
                     break;
              }
              else
             {
                     for(int i = -1;i <= 1;i++)
                     {
                              if(0 == i)continue;
                              if(tmp.x + i >= 0 && tmp.x + i < n)
                             {
                                        if(visit[tmp.x+i][tmp.y] == 0 && prison[tmp.x+i][tmp.y] != ‘#‘)
                                        {
                                                visit[tmp.x+i][tmp.y] = 1;
                                                p1.x = tmp.x + i;
                                                p1.y = tmp.y;
                                                high[p1.x][p1.y] = high[tmp.x][tmp.y] + 1;
                                                road.push(p1);
                                        }
                              }
                             if(tmp.y + i >= 0 && tmp.y + i < m)
                            {
                                       if(visit[tmp.x][tmp.y+i] == 0 && prison[tmp.x][tmp.y+i] != ‘#‘)
                                      {
                                                visit[tmp.x][tmp.y+i] = 1;
                                                p1.x = tmp.x;
                                                p1.y = tmp.y + i;
                                                high[p1.x][p1.y] = high[tmp.x][tmp.y] + 1;
                                                road.push(p1);
                                       }
                            }
                      }
                }
        }
        if(flag)
       {
             printf("%d\n",high[finalX][finalY]);
       }
       else printf("Poor ANGEL has to stay in the prison all his life.\n");
}

int main(void)
{
        while(scanf("%d%d",&n,&m) != EOF)
       {
               for(int i = 0;i < n;i++)
               scanf("%s",prison[i]);
               init();
               BFS();
       }
       return 0;
}

时间: 2024-10-13 15:52:35

【原创】ZOJ_1649 Rescue 解题报告的相关文章

【原创】leetCodeOj --- Sliding Window Maximum 解题报告

天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

解题报告 之 CodeForces 6E Exposition

解题报告 之 CodeForces 6E Exposition Description There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction

习题:codevs 2822 爱在心中 解题报告

这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联通分量里面只有这一道题. 题目是这样的: “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有着一个爱的名单,上面记载着他所爱的人(不会出现自爱的情况).爱是

解题报告 之 HDU5301 Buildings

解题报告 之 HDU5301 Buildings Description Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each buil

hdu - 5349 MZL&#39;s simple problem(解题报告)

A - MZL's simple problem Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number

解题报告 之 HDU5294 Tricks Device

解题报告 之 HDU5294 Tricks Device Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the entrance of the tomb while Dumb Zhang's at the end of it. The tomb is made up of many chambers, the total number is N. And there are M c

解题报告 之 HDU5335 Walk Out

解题报告 之 HDU5335 Walk Out Description In an  maze, the right-bottom corner is the exit (position  is the exit). In every position of this maze, there is either a  or a  written on it. An explorer gets lost in this grid. His position now is , and he wan

解题报告 之 HDU5325 Crazy Bobo

解题报告 之 HDU5325 Crazy Bobo Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight . All the weights are distrinct. A set with m nodes  is a Bobo Set if: - The subgraph of his tree induced by this set is