RescueTime limit: 2000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 13 accepted submission (s): 10 font: times New Roman | verdana | Georgia font size: Drawing → 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 Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample output
13 because of the existence of a guard, it may not be a small time to first join the queue. If we want to output a small amount of time, we need to first deal with a small amount of time. Therefore, the priority queue is used. If the time is small, the priority is given. # Include <iostream> # include <stdio. h> # include <queue> # include <string. h> using namespace STD; # define M 210 struct node {int X, Y, time; friend bool operator <(node A, Node B) // custom priority queue (I think so) {return. time> B. time; // opposite to sort. time> B. in time, B enters the team first, that is, the time is small. }; Int n, m, vis [m] [m]; char map [m] [m]; int dis [4] [2] =, -1, 0, 0,-1}; int DFS (int x, int y, int time) {int I, j; node T, temp; memset (VIS, 0, sizeof (VIS); T. X = x; T. y = y; T. time = time; priority_queue <node> q; // defines a priority queue. Q. Push (t); vis [T. X] [T. Y] = 1; while (! Q. empty () {T = Q. top (); q. pop (); If (Map [T. x] [T. y] = 'A') {return T. time ;}for (I = 0; I <4; I ++) {temp. X = T. X + dis [I] [0]; temp. y = T. Y + dis [I] [1]; temp. time = T. time + 1; if (temp. x> = 0 & temp. x <n & temp. y> = 0 & temp. Y <m) // determine whether the cross-border traffic is crossed first. If (! Vis [temp. x] [temp. Y] & map [temp. x] [temp. Y]! = '#') // Judge whether the combination is invalid. Otherwise, an error is returned. {If (Map [temp. x] [temp. Y] = 'X') // overguard time and add one. Temp. time ++; vis [temp. x] [temp. y] = 1; q. push (temp) ;}}return-1 ;}int main () {int I, j, k; int FX, FY; while (CIN >>n> m) {for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {CIN> map [I] [J]; If (Map [I] [J] = 'R') FX = I, FY = J ;} I = DFS (FX, fy, 0); if (I> = 0) printf ("% d \ n", I ); else printf ("Poor Angel has to stay in the prison all his life. \ n ") ;}return 0 ;}
HDU 1242 rescue (BFS + priority queue)