Zoj 1649: Rescue (BFS)

Source: Internet
Author: User

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 Wils, 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 shocould output a single integer, standing for the minimal time needed. if such a number does no exist, you showould 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


This is to say that angle has been captured and her friends have helped her .. X is an obstacle to his enemies, but angle's friends are strong enough to destroy all enemies, but while at the same time eliminating the enemy,

It takes one second for him to save the angle ..

This question uses BFs, but it should be noted that the shortest path may not be the optimal solution ..

Therefore, we define a two-dimensional array to record the shortest time for each location. When a BFS search arrives at a location, the search results are not queued when the shortest time is greater than the shortest time.

This solves the problem of this question.

In addition, we recommend that you do not publish BFS return results when you determine the target location .. That's why I ran wa here .. This may not be the optimal solution .. Only when the queue is empty or

The optimal solution or "unable to reach the destination location" can be obtained only when the BFS search ends ".


# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <cmath> using namespace STD; const int maxn = 205; const int INF = 100000; // used for the initial time at each location, open as big as possible char map [maxn] [maxn]; // map int mintime [maxn] [maxn]; // records the minimum time int n, m for each location; // map size int ax, ay; // Angel position int FX, FY; // Angel's friend position int ans; // result // map walk method int DX [] = {-1, 0, 0, 1}; int dy [] = {0,-1, 1, 0}; s Truct point {int X; int y; // int time in the Square; // record the time spent currently arriving at this point}; queue <point> q; // queue int BFS () // BFS function {While (! Q. empty () {point temp = Q. front (); // record the first element Q. pop (); // If (temp. X = AX & temp. y = Ay) // return mintime [ax] [AY]; start with output here to make wa .. 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 the border and the 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 an enemy is encountered, add 1 If (next. time <mintime [XX] [YY]) // if the time used for this walk is smaller than the original mintime, enter the queue {mintime [XX] [YY] = next. time; q. push (next) ;}}}return mintime [ax] [AY]; // return result} void output () // output {If (ANS <inf) printf ("% d \ n", ANS); else printf ("Poor Angel has to stay in the prison all his life. \ n ");} void Init () // input and initialization {While (scanf (" % d ", & N, & M) = 2) {memset (MAP, 0, sizeof (MAP); For (INT I = 0; I <n; I ++) scanf ("% s ", map [I]); // read the map for (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) {mintime [I] [J] = inf; // the initial time of each location if (Map [I] [J] = 'R ') // record the location of friend {FX = I; FY = J;} If (Map [I] [J] = 'A ') // record Angel location {ax = I; ay = J ;}} point stay; stay. X = FX; stay. y = FY; stay. time = 0; mintime [FX] [FY] = 0; q. push (stay); // start of BFS ans = BFS (); output () ;}} int main () {Init (); Return 0 ;}








Zoj 1649: Rescue (BFS)

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.