toj-1335-Rescue Angel

Source: Internet
Author: User
Tags time limit

This question once let me put in the commemoration of Infinity here.

Rescue Angel

Time Limit (Common/java): 1000ms/10000ms Memory Limit:65536kbyte
Total submit:236 accepted:56

Description

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, "X" stands for a guard, 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

Test Instructions Description

Angel (a) trapped in the maze, its friend (R) to save her, there will be guards in the Maze (x). R each step takes a unit of time, if the road encounters X, Kill X will need a unit of time, to find the shortest time a. If it is not found, the output "Poor ANGEL have to stay in the prison all he life."

Thinking of solving problems

This question is a maze problem, solving the shortest path. The use of breadth-first search solvable, but the attention to detail, is the order of the search to bring a series of problems.

Take a look at this set of data:
3 3
#.r
#.x
#a #

If the wide search is carried out in the left and right order, then the queue will first be added (0,1). Then, to start with, make a wide search, first let the time +1, because the guard, and then to the surrounding search, to the queue to join (a), and mark it as "visited" ....

If this is the search, then the shortest time will be 4 instead of 3. Because (first) is marked with a path from X to access, then the (0,1) point over the path will not be able to access to (a)! Causes the shortest circuit to become 4 instead of 3.

In the same vein, there is this set of data:

4 3

.. R

. #x

. #x

. Ax

If the result is 7, you need to be aware of it.


There is also a loophole in this question, that is, although there are multiple (for each of Angle's friend) friends (starting point), the test data has only one friend (starting point). Of course, if there is a number of friends, it is not difficult, only a few times to call BFs to find the smallest one.

Test Data

7 8

#.#####.

#.a#. R.

#.. #x ...

.. #.. #.#

#...##..

.#......

........

13

7 8

#.#####.

#.a#. R.

#.. #x ...

.. #.. #.#

##.. ##..

.#......

........

Poor ANGEL have to stay in the prison all he life.

2 8

#.#####.

#.a#. R.

Poor ANGEL have to stay in the prison all he life.

4 8

#.#####.

#.a#. R.

#.. #x ...

.....#.#

9

3 3

#.r

#.x

#a #

3

3 3

#xr

#..

#a #

3

3 3

.. R

. #x

. A.

4

4 3

.. R

. #x

. #x

. Ax

6

Source Code g++-15ms

1.true false notation

#include <cstdio>

#include <queue>

using namespace Std;

const INT direction[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//Four Directions

int row,col;//rows, columns

int startr,startc;//starting point column

Char maze[200][201];//Maze

BOOL visit[200][201];//Tag

int BFS ()//Breadth First Search

{

Queue<int> Open;

Open.push (STARTR);//Press

Open.push (STARTC);//pressure into row

Open.push (0);//Press-in time

Visit[startr][startc]=true;

while (!open.empty ())

{

int R=open.front ();

Open.pop ();

int C=open.front ();

Open.pop ();

int T=open.front ();

Open.pop ();

if (maze[r][c]== ' a ')//Find the end

{

return t;

}

else if (maze[r][c]== ' x ')//Encounter guard

{

Increased combat time by 1 units

Maze[r][c]= '. ';

Open.push (R);

Open.push (c);

Open.push (t+1);

continue;//because of the need for a combat time,

Therefore, the number of search steps to delay one step, so that other better search first, in case of errors.

}

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.