HDOJ1242 Rescue (Rescue), hdoj1242rescue

Source: Internet
Author: User

HDOJ1242 Rescue (Rescue), hdoj1242rescue
Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 22286 Accepted Submission (s): 7919


Problem DescriptionAngel 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 .)

 

InputFirst 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.

 

OutputFor 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 Input7 8 #. #####. #. a #.. r. #.. # x ..... #.. #. ##... ##... #..............

 

Sample Output13 In the BFS search process, you cannot exit the BFS process as soon as the target location is reached; otherwise, the obtained result is only the minimum number of steps from r to. Because BFS searches for vertices Based on depth, BFS first searches for the lowest number of steps, not necessarily the optimal solution, and the time may be longer. You must wait until the linked list is empty and the BFS search process ends to obtain the optimal solution or draw a conclusion that the target location cannot be found. This question does not determine whether the location has been accessed, but does not go through an infinite loop. This is because determining whether to add adjacent locations (x, y) to the column from a certain position requires less time for this walk than before (x, y; if less time is used, the (x, y) position will repeat the column, but will not be infinite. 1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 using namespace std; 5 # define MAX 1000000 6 int Map [250] [250]; 7 int T [250] [250]; 8 int dir [4] [2] = {}, {-}, {}, {0, -1 }}; 9 int n, m; 10 int si, sj, di, dj; 11 int sign = 0; 12 typedef struct pointer13 {14 int x, y; 15 int time; 16 struct pointer * next; 17} LNode, * LinkList; 18 LinkList ptr; 19 void bfs (LinkList head); 20 int main () 21 {22 int I, j; 23 while (scanf ("% d", & n, & m )! = EOF) 24 {25 getchar (); 26 for (I = 0; I <n; I ++) 27 {28 for (j = 0; j <m; j ++) 29 {30 scanf ("% c", & Map [I] [j]); 31 T [I] [j] = MAX; 32 if (Map [I] [j] = 'A') 33 {34 di = I; 35 dj = j; 36} 37 else if (Map [I] [j] = 'R') 38 {39 si = I; 40 sj = j; 41 T [si] [sj] = 0; 42} 43} 44 getchar (); 45} 46 LinkList p; 47 p = (LinkList) malloc (sizeof (LNode )); 48 p-> x = si; 49 p-> y = sj; 50 p-> time = 0; 51 p-> next = NULL; 52 sign = 0; 53 bfs (p); 54 if (T [di] [dj] <MAX) cout <T [di] [dj] <Endl; 55 else cout <"Poor ANGEL has to stay in the prison all his life. "<endl; 56} 57 return 0; 58} 59 void bfs (LinkList head) 60 {61 int I, fx, fy; 62 while (head! = NULL) 63 {64 for (I = 0; I <4; I ++) 65 {66 fx = head-> x + dir [I] [0]; 67 fy = head-> y + dir [I] [1]; 68 if (fx> = 0 & fx <n & fy> = 0 & fy <m & Map [fx] [fy]! = '#') 69 {70 LinkList p; 71 if (sign = 0) ptr = head; 72 p = (LinkList) malloc (sizeof (LNode )); 73 p-> x = fx; 74 p-> y = fy; 75 p-> time = head-> time + 1; 76 if (Map [fx] [fy] = 'X') p-> time ++; 77 if (p-> time <T [fx] [fy]) 78 {79 T [fx] [fy] = p-> time; 80 ptr-> next = p; 81 p-> next = NULL; 82 ptr = ptr-> next; 83 sign = 1; 84} 85} 86} 87 head = head-> next; 88} 89}View Code

 

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.