Zoj 1649 & HDU 1242 rescue

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


Author: Chen, Xue

Source: zoj monthly, October 2003.


Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 649

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242


The Angel is imprisoned .... his ...) to save him, please try the shortest time. There are walls in the dungeon with street guards. Suppose every guard will be defeated (this guard is really rubbish), and every time it takes to defeat a guard plus 1.


Question analysis: the blogger has been doing this for more than two hours. I am going to write down the mental journey of this question. It is also quite hard to write a blog here without going to bed at half past two in the middle of the night, we started to use BFs to record the number of steps directly. We found that the sample had passed wa, but the bloggers had brains and could ignore it... then, use the time table corresponding to the graph to record the shortest time of each vertex, and finally return to the shortest time of the requested vertex. The example is over, and a is returned, it was just a's on zoj, and I almost had to write a blog. I was about to go to HDU and try again... as a result, it took more than an hour to complete the water. We could only say that zoj's data was too watery. Later we found that (Angel's friends ), ("R" stands for each of Angel's friend .) in these two sentences, the blogger vomited blood on the spot... it's all about questions... angel is so awesome, there will certainly be many friends, no way, continue to change, still using time records, multiple BFs, what is the result ?, In HDU, not only is it a 0 s, but it is still 30 ms on zoj, so the data on these two OJ should be integrated... after reading various online versions, the problem solution is BFS + priority queue. If BFS + priority queue is used, you need to find a friend from the Angel. My method doesn't matter. I will post the code below, several groups of data that are self-organized by the blogger


7 8

#.#####.

# A. # XR ..

#. X # XX ..

#.......

.#......

........

12


4 5

Axxxr

.. X ..

#. X.

Rxxr #

6


5 7

#######

#... Xx #

# A ## XR #

#... Xx #

#######

8


# Include <cstdio> # include <cstring> # include <queue> using namespace STD; int const max = 200 + 1; int const INF = 0 xFFFF; char map [Max] [Max]; int min_t [Max] [Max]; int dirx [4] = {1,-1, 0, 0 }; int diry [4] = {,-}; int ax, ay, RX, Ry; int M, N; struct point {int X, Y; int t ;}; int BFS () {queue <point> q; point cur; cur. X = RX; cur. y = ry; cur. T = 0; q. push (cur); While (! Q. empty () {cur = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {point next = cur; next. X + = dirx [I]; next. Y + = diry [I]; next. t ++; // Add 1 If (Map [next. x] [next. y] = 'X') Next. t ++; If (next. x <1 | next. x> N | next. Y <1 | next. y> M | map [next. x] [next. y] = '#') continue; // if the current time is less than the shortest time recorded by the current point, update the shortest time value of the Change Point // and press the point into the queue if (next. T <min_t [next. x] [next. y]) {min_t [next. x] [next. y] = next. t; q. pus H (next) ;}}return min_t [ax] [AY]; // returns the shortest time from the current friend to the angel} int main () {int ans; while (scanf ("% d", & N, & M )! = EOF) {ans = inf; memset (MAP, 0, sizeof (MAP); For (INT I = 1; I <= N; I ++) // input graph {scanf ("% s", map [I] + 1); For (Int J = 1; j <= m; j ++) {min_t [I] [J] = inf; // initialize the time table to the maximum value. If (Map [I] [J] = 'A') {ax = I; ay = J ;}}for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) if (Map [I] [J] = 'R') // search for each R. {RX = I; ry = J; if (ANS> BFS () ans = BFS ();} If (ANS = inf) printf ("Poor Angel has to stay in the prison all his. Life. \ n "); else printf (" % d \ n ", ANS );}}


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.