Hdoj 1242 Rescue (BFS)

Source: Internet
Author: User

Hdoj 1242 Rescue (BFS)

 

Rescue Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1242
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 18962 Accepted Submission (s): 6771



Problem Description 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 and October 2003
Recommend Eddy | We have carefully selected several similar problems for you: 1240 1016 1010 1072 /**/Write BFS for the first time

 

Question:

1. Find the shortest time or number of steps from one point to one in the maze with obstacles,

2. In this question, there are more than one friend of the angel, so the search start point should be from the angel to the friend,

3. The question is the shortest time, So priority queue should be used only, and priority should be given to the number of small steps.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------

/**/BFS:

1. Access V0 from a V0 first;

2. Access all vertices v1, v2, and Vt adjacent to v0;

3. Access all unaccessed vertices adjacent to v1, v2, and Vt in sequence;

4. Follow the previous steps until all vertices have been accessed;

 

BFS operations:

Define a queue; queue Q;

Add the start point to the queue; Q. push (p );

While (the queue is not empty) while (! Q. empty ())

{{

Retrieve the head node of the team; p = Q. front; Q. pop;

If the desired target State is if (the condition is met)

Jump out of loop; return;

Otherwise, else

Expand the child node from it,

Add all to team end Q. push ();
}}

If the target is found in the loop, the result is output; otherwise, no solution is output;

Certificate ----------------------------------------------------------------------------------------------------------------------------------------

# Include

# Include
# Include
# Include
# Include
# Include
Using namespace std;
Const int M = 205;
Int n, m;
Int start_x, end_x;
Int start_y, end_y;



Char map [M] [M];
Int cost [M] [M];
Int fx [4] = {1, 0,-1, 0 };
Int fy [4] = {0, 1, 0,-1 };


Typedef struct
{
Int x, y;
Int time;
} Point;


Void init ()//
{
Int I, j;
For (I = 0; I For (j = 0; j {
If (map [I] [j] = 'R') // access end
{
Start_x = I;
Start_y = j;
}
If (map [I] [j] = 'A') // output end
{
End_x = I;
End_y = j;
}
Cost [I] [j] =-1;
}
}


Int isvalid (int x, int y)
{
If (x> = 0 & x = 0 & y Return 1;
Else
Return 0;
}




Void BFS ()
{
Point p1, p2;
Queue Q; // define a queue
Int I;

P1.x = start_x; // Initialization
P1.y = start_y;
P1.time = 0;
Cost [p1.x] [p1.y] = 0;

While (! Q. empty () // Initialization
Q. pop ();

Q. push (p1); // start point to join the queue
While (! Q. empty () // when the queue is not empty
{
P1 = Q. front (); // retrieve the header node Information
Q. pop ();

For (I = 0; I <4; I ++) // meets the conditions
{
P2.x = p1.x + fx [I];
P2.y = p1.y + fy [I];
P2.time = p1.time + 1;

If (map [p2.x] [p2.y] = '#' |! Isvalid (p2.x, p2.y) // meets the conditions
Continue;
If (map [p2.x] [p2.y] = 'X ')
P2.time ++;
If (cost [p2.x] [p2.y] =-1 | p2.time {
Q. push (p2); // join
Cost [p2.x] [p2.y] = p2.time;
}
}
}
}





Int main ()
{
While (~ Scanf ("% d", & n, & m ))
{
Int I, j;
For (I = 0; I Scanf ("% s", map [I]);

Init ();
BFS ();
If (cost [end_x] [end_y] =-1)
Printf ("Poor ANGEL has to stay in the prison all his life. \ n ");
Else
Printf ("% d \ n", cost [end_x] [end_y]);

}
Return 0;
}

Related Article

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.