HDU 1242 Rescue

Source: Internet
Author: User

RescueTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 10194 Accepted Submission (s ): 3728 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 li Nes 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 Indium Ut7 8 #. #####. #. a #.. r. #.. # x ..... #.. #. ##... ##... #.............. sample Output13 AuthorCHEN, Xue SourceZOJ Monthly, October 2003 RecommendEddy solution: Memory BFS, using an array to maintain the minimum value of a fixed point, but this question has a pitfall, that is, there may be multiple friends of the hero, so we must start from the hero to find the minimum value of his to all friends, that is, the answer. [Cpp] # include <iostream> # include <cstring> # include <queue> using namespace std; # define INF 99999 char maze [205] [205]; long len [205] [205]; int mov [4] [2] ={{}, {-}, {}, {0,-1 }}; // R, L, D, U void BFS (int xa, int ya) {int x0, y0, j, p, q; queue <int> x; queue <int> y; x. push (xa); y. push (ya); while (! X. empty ()&&! Y. empty () {x0 = x. front (); y0 = y. front (); x. pop (); y. pop (); for (j = 0; j <4; j ++) {// The following section contains pruning, that is to say, if there is a shorter path, add this path to the queue and search for p = x0 + mov [j] [0]; q = y0 + mov [j] [1]; if (maze [p] [q] = '. '| maze [p] [q] = 'R' | maze [p] [q] = 'X ') {if (maze [p] [q] = 'X ') // directly killing the guard is faster than bypassing the road or even killing it as soon as the guard goes out. {if (len [x0] [y0] + 2 <len [p] [q] | len [p] [q] = 0) {len [p] [q] = len [x0] [y0] + 2; x. push (p); y. push (q) ;}} else if (len [x0] [y0] + 1 <len [p] [q] | len [p] [q] = 0) // if the original path to this point is farther than that from the search point, or the point is not found yet, change the shortest distance to this point {len [p] [q] = len [x0] [y0] + 1; x. push (p); y. push (q) ;}}}} int main () {int m, n, I, j, a, B; long ans; while (cin> m> n) {ans = INF; memset (maze, '*', sizeof (maze); memset (len, 0, sizeof (len); for (I = 1; I <= m; I ++) for (j = 1; j <= n; j ++) {cin> maze [I] [j]; if (maze [I] [j] = 'A') {a = I; B = j ;}} BFS (a, B); for (I = 1; I <= m; I ++) for (j = 1; j <= n; j ++) www.2cto.com if (maze [I] [j] = 'R' & ans> len [I] [j] & len [I] [j]) // if len is 0, ans = len [I] [j]; if (ans = INF) // ans failed to update the description, indicating that the hero could not touch his friends (guys) cout <"Poor ANGEL has to stay in the prison all his life. "<endl; else cout <ans <endl;} 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.