Zoj 1649 rescue (BFS + Queue)

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


The queue is widely searched, and an array is used to record the minimum time taken to the current position, so as to determine whether to queue the current status.

# Include "stdio. H "# include" string. H "# include" queue "# include" algorithm "using namespace STD; # define n 205 # define INF should have int EI, EJ, n, m; // The target position and row/column range: int dir [4] [2] = {, 0,-1,-,}; int mark [N] [N]; // The minimum time used to record the current position: Char G [N] [N]; struct node {int X, Y, t ;}; int BFS (int x, int y) {int I; queue <node> q; node cur, next; cur. X = x; cur. y = y; cur. T = 0; q. push (cur); Mark [x] [Y] = 0; while (! Q. empty () {cur = Q. front (); q. pop (); for (I = 0; I <4; I ++) {next. X = x = dir [I] [0] + cur. x; next. y = dir [I] [1] + cur. y; If (x <0 | x> = n | Y <0 | Y> = M | G [x] [Y] = '#') continue; next. T = cur. t + 1; if (G [x] [Y] = 'X') Next. t ++; If (next. T <mark [x] [Y]) {mark [x] [Y] = next. t; q. push (next) ;}}// because no priority queue is used, the optimal solution can be obtained only when the queue is empty. If (MARK [ei] [EJ]! = Inf) return mark [ei] [EJ]; Return-1 ;}int main () {int I, j, Si, SJ; while (scanf ("% d", & N, & M )! =-1) {for (I = 0; I <n; I ++) {scanf ("% s", G [I]); For (j = 0; j <n; j ++) {mark [I] [J] = inf; // It is initialized as an infinitely large if (G [I] [J] = 'A ') {Si = I; SJ = J;} If (G [I] [J] = 'R') {Ei = I; EJ = J ;}}} int T = BFS (Si, SJ); If (t =-1) printf ("Poor Angel has to stay in the prison all his life. \ n "); else printf (" % d \ n ", T);} 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.