ZOJ 1649:rescue (BFS)

Source: Internet
Author: User
Tags time limit

Rescue time Limit:2 Seconds Memory limit:65536 KB

Angel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.

Angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or his?) 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 had 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-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 the 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 have to stay in the prison all he life."


Sample Input

7 8
#.#####.
#.a#. R.
#.. #x ...
.. #.. #.#
#...##..
.#......
........


Sample Output

13

The problem is that angle was caught and her friend went to save her. And X is a hindrance to his enemies, but Angle's friends are strong enough to destroy all enemies, but while destroying the enemy,

He's going to take 1 more seconds to find out the shortest amount of time he needs to save angle.

This problem uses BFS, but note that the shortest path may not be the optimal solution.

So we define a two-dimensional array to record the shortest time of each position, in the process of BFS search, to a certain location, the results of the search is greater than the shortest time, do not queue.

This solves the problem of the subject.

In addition, do not make a decision to arrive at the destination location when the BFS returned results. That's why I'm here in WA. Because this may not be the optimal solution. Only wait until the queue is empty or

At the end of the BFS search, the optimal solution or "unreachable destination" can be obtained.



#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <

vector> #include <queue> #include <cmath> using namespace std;
const int MAXN = 205; the const int INF = 100000;//is used for the initial time of each position, as large as possible char map[maxn][maxn];//map int mintime[maxn][maxn];//Record the minimum time for each position int n, m;//to
Figure size int ax, ay;//angel position int FX, Fy;//angel friend's position int ans;//result//map walk int dx[] = {-1, 0, 0, 1};

int dy[] = {0,-1, 1, 0};
    struct point {int x;
int y;//The position of the square int time;//record the time it takes to go to the current}; queue<point>q;//Queue int BFS ()//bfs function {while (!
        Q.empty ()) {Point temp = Q.front ();//Record team first element Q.pop (); if (Temp.x==ax && temp.y==ay)//return Mintime[ax][ay]; Start me in this output, make WA.
            Because this may not be the optimal solution for (int i=0; i<4; i++) {int xx = temp.x + dx[i];
            int yy = temp.y + dy[i]; if (xx>=0 && xx<n && yy>=0 && yy<m && Map[XX]
                [yy]!= ' # ')//exclude boundary and wall {point next;//next position next.x = XX;
                Next.y = yy;
                Next.time = temp.time + 1; if (map[xx][yy] = = ' x ') Next.time + = 1;//If you encounter an enemy, eliminate the need to add 1 if (Next.time < MINTIME[XX][YY])//If this method takes more time than the original
                    Mintime Small the queue {mintime[xx][yy] = Next.time;
                Q.push (next); }}}} return mintime[ax][ay];//result} void output ()//output {if (ans < INF) pr
    intf ("%d\n", ans);
else printf ("Poor ANGEL have to stay in the prison all his life.\n");
        } void Init ()//input and initialization {while (scanf ("%d%d", &n, &m) ==2) {memset (map, 0, sizeof (map)); for (int i=0; i<n; i++) scanf ("%s", Map[i]);//read in map for (int i=0; i<n; i++) for (int j=0; j<m;
            J + +) {Mintime[i][j] = inf;//the time at which each position was initially    if (map[i][j] = = ' R ')//record friend's location {fx = i;
                Fy=j;
                    } if (map[i][j] = = ' a ')//record Angel position {ax = i;
                ay = j;
        }} point stay;
        stay.x = FX;
        STAY.Y = FY;
        Stay.time = 0;
        Mintime[fx][fy] = 0;
        Q.push (stay);//bfs's Start ans = BFS ();
    Output ();

    }} int main () {init ();
return 0;
 }








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.