Zoj 1649 & HDU 1242 rescue (BFS + priority queue)

Source: Internet
Author: User
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


Resolution:

In order to ensure that the BFS tree is stretched out at an equal distance, this is not an equal distance, so some processing is required.

1. My method is to find the angel, compare the time to the lower size, and finally output the smallest value. Optimization is required. In this case, it will be TLE's. If it passes through a grid, the grid will save the time when it passes, and the next time it will go to this grid. If the time is shorter than the time in the grid, otherwise, you do not need to enter the team. 20 ms.

2. If the priority queue + BFS is BFs at an equal distance, the time value in the queue is arranged from small to large, so you can directly use the priority queue for 0 ms.


The code and detailed parsing are as follows:
Method I:
/***** Zoj 1649 *****/# include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <set> # include <queue> # include <algorithm> # define maxn 210 # define INF 1000000 // not too large, too large result Error # define RST (n) memset (n, 0, sizeof (N) using namespace STD; struct {int X, Y; int step; int time ;} Q [1000000]; char map [maxn] [maxn]; // map int Min [maxn] [maxn]; // minimum time required to go to each location: int front, rear, res; int n, m, Sx, Sy, ex, E Y; const int DX [] = {-1, 0, 1, 0}; // direction array const int dy [] = {0, 1, 0,-1 }; bool check (int x, int y) // The condition {return x> = 0 & Y> = 0 & x <n & Y <M & map [x] [Y] for the next step. = '#';} Void Init () {front = rear = 0; memset (Min, INF, sizeof (min )); // initialize Min [SX] [sy] = 0; Q [rear]. X = Sx, Q [rear]. y = sy; Q [rear]. time = 0, Q [rear ++]. step = 0; // Add the starting position to the queue} int main (INT argc, char * argv []) {While (~ Scanf ("% d", & N, & M) {for (INT I = 0; I <n; I ++) {scanf ("% s ", map [I]); For (Int J = 0; Map [I] [J]; j ++) {If (Map [I] [J] = 'A') {EX = I, ey = J ;} // record end position else if (Map [I] [J] = 'R') {SX = I, Sy = J ;} // record start position} Init (); // initialization of individual data while (front <rear) {// when the queue is not empty int PX = Q [Front]. x; int py = Q [Front]. y; // current position for (INT I = 0; I <4; I ++) {int xx = px + dx [I]; int YY = py + dy [I]; // printf ("xx = % d YY = % d \ n", XX, YY); If (check (XX, YY) {// printf ("matched xx = % d, YY = % d \ n", XX, YY); // v [XX] [YY] = 1; int Qt = Q [Front]. time + 1; if (Map [XX] [YY] = 'X') Qt ++; // if it is a guard, kill, time + 1 If (qt <min [XX] [YY]) {// if this walk takes less time than the previous walk to this location, join the queue, otherwise, you do not need to join the queue Min [XX] [YY] = QT; Q [rear]. X = XX, Q [rear]. y = YY; Q [rear]. step = Q [Front]. step + 1; Q [rear ++]. time = QT;} // printf ("matched cur_char = % C \ n", map [PX] [py]); // printf ("matched next_pos_char = % C \ n", map [XX] [YY]); // printf ("matched cur_step = % d, cur_time = % d \ n ", Q [Front]. step, Q [Front]. time); // printf ("matched step = % d, time = % d \ n", Q [rear]. step, Q [rear]. time) ;}} front ++;} If (Min [Ex] [ey] <inf) printf ("% d \ n", Min [Ex] [ey]); else puts ("Poor Angel has to stay in the prison all his life. ");} return 0 ;}

Method II:
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <limits. h> # include <set> # include <queue> # include <algorithm> # define maxn 210 # define RST (n) memset (n, 0, sizeof (n )) using namespace STD; typedef struct node {int X, Y; int time ;}node; node start; priority_queue <node> PQ; char map [maxn] [maxn]; int V [maxn] [maxn], n, m, Res; const int DX [] = {-1, 0, 1, 0 }; const int dy [] = {0, 1, 0,-1}; bool operator <(node A, Node B) {return. time> B. time;} bool check (int x, int y) {return x> = 0 & Y> = 0 & x <n & Y <M & map [x] [Y]! = '#'&&! V [x] [Y];} int BFS () {node cur; int XX, YY, PX, Py, T; while (! PQ. empty () {cur = PQ. top (), PQ. pop (); PX = cur. x, Py = cur. y, t = cur. time; For (INT I = 0; I <4; I ++) {xx = px + dx [I], YY = py + dy [I]; if (check (XX, YY) {v [XX] [YY] = 1; if (Map [XX] [YY] = 'A') return t + 1; // meet the friends and return to cur. X = XX, cur. y = YY, cur. time = t + 1; if (Map [XX] [YY] = 'X') cur. time ++; // killed the guard, time + 1 PQ. push (cur); // enter the queue} return-1;} int main (INT argc, char * argv []) {While (~ Scanf ("% d", & N, & M) {RST (V); While (! PQ. empty () PQ. pop (); // clear the queue for (INT I = 0; I <n; I ++) {scanf ("% s", map [I]); for (Int J = 0; j <m; j ++) {If (Map [I] [J] = 'R') {// records the initial position start. X = I, start. y = J; start. time = 0; PQ. push (start); // enter the team V [I] [J] = 1 ;}}res = BFS (); If (res! =-1) printf ("% d \ n", Res); else puts ("Poor Angel has to stay in the prison all his life.");} return 0 ;}


Zoj 1649 & HDU 1242 rescue (BFS + priority queue)

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.