(Hdu step 4.2.3) Rescue (calculate the minimum number of steps from the start point to the end point, it takes 2 seconds to encounter a special node), hdu4.2.3

Source: Internet
Author: User

(Hdu step 4.2.3) Rescue (calculate the minimum number of steps from the start point to the end point, it takes 2 seconds to encounter a special node), hdu4.2.3

Question:

Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 924 Accepted Submission (s): 387
 
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.

 
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
 
AuthorCHEN, Xue
SourceZOJ Monthly, October 2003.
RecommendEddy


Question Analysis:

Search. This question needs to be done using the priority queue. Because normal queues cannot ensure that each pair is the least time element.



The Code is as follows:

/** B. cpp ** Created on: February 17, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <queue> using namespace std; const int maxn = 205; // Maximum Matrix range: const int inf = 999999; char map [maxn] [maxn]; // original map int vis [maxn] [maxn]; // stores the minimum number of steps that reach a node. for example, vis [I] [j] = 9. it indicates that the minimum number of steps for accessing the node in column j of row I is 9 steps int n, m; // number of rows, number of columns int x1, y1; // The abscissa and ordinate int x2 and y2 of the start point; // the abscissa of the end point and the ordinate int dir [4] [2] = {// direction matrix }, {}, {}, {0,-1 }};/ /Node struct Node {int x; // abscissa int y; // ordinate int step; // number of steps to reach this Node friend bool operator <(Node a, Node B) {// sort the priority queue in ascending order. Return. step> B. step ;//.... note This writing method ..}}; /*** determine whether the next step is valid */bool check (int x, int y) {if (x <0 | x> = n | y <0 | y> = m) {// determine whether to return false beyond the permitted range ;} if (map [x] [y] = '#') {// determines whether the obstacle is returned false;} return true; // indicates that the next step is legal ..} /*** wide search */int bfs () {priority_queue <Node> q; Node root; // initialize the root Node accordingly. x = x1; root. y = y1; root. step = 0; vis [x1] [y1] = 0; Node k; q. push (root); // Add the root node to the queue while (! Q. empty () {// If the queue is not empty Node a = q. top (); // retrieve the first element q. pop (); if (. x = x2 &. y = y2) {// if the return. step; // return the minimum number of steps} int I; for (I = 0; I <4; ++ I) {// traverse to find the next level element k of the node. x =. x + dir [I] [0]; k. y =. y + dir [I] [1]; if (check (k. x, k. y) = false) {// determine whether the next-level element is valid for continue;} // if the following code can be executed, the node is valid for k. step =. step + 1; // calculate the number of steps to the node if (map [k. x] [k. y] = 'X') {// If a guard k is encountered. step + = 1; // The number of required steps + 1} if (vis [k. x] [k. y]> k. step) {// if you have arrived before The number of steps of a vertex> the number of steps to reach the vertex calculated at present. vis [k. x] [k. y] = k. step; // the minimum number of steps q. push (k); // enter the element into the queue} return 0; // return this indicates that no path has reached the end} int main () {while (scanf ("% d", & n, & m )! = EOF) {int I; int j; for (I = 0; I <n; ++ I) {scanf ("% s", map [I]); // pay attention to this writing method... Scanf ("% c", & map [I] [j]) always has this problem for (j = 0; j <m; ++ j) {if (map [I] [j] = 'A') {// mark the end point x2 = I; y2 = j ;} if (map [I] [j] = 'R') {// mark the start point x1 = I; y1 = j ;}} memset (vis, inf, sizeof (vis); // minimum number of steps for initialization to reach each point int ans = bfs (); if (ans = 0) {// If angleprintf ("Poor ANGEL has to stay in the prison all his life. \ n "); // The output cannot find the corresponding statement} else {// if you can find the princess printf (" % d \ n ", ans ); // The minimum number of steps required to find the princess is output} 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.