Rescue
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 14966 accepted submission (s): 5425
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
Angel's friend wants to save the angel from the prison. How does it take the least time? A represents an angel, R represents an angel's friend, and X represents a monster, angel friends can kill monsters, but it takes more time to spend 2, and # represents the wall ,. it indicates the path that can be taken. It takes 1 time. You need to find the minimum time for saving the angel. If not, output-1.
Solution: Based on my own ideas, if you do not need to prioritize queues, search by BFS is the first to find, not necessarily the shortest, in addition, all the paths that were first found were marked, leading to the possibility that the shortest would not be able to go any longer. In this way, I contributed to WA several times. Then, if we use the priority queue, the minimum number of steps in each step is kept in the header of the queue, so that the number of steps found must be the smallest. Another prompt is: Q. Front () is the first to take the lead, and Q. Top () is the first to take the lead in the priority queue. Remember it next time. Hey.
Paste the Code:
# Include <stdio. h >#include <queue> # define maxn 0x3f3f3fusing namespace STD; char map [205] [205]; int dir [4] [2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // int visited [205] [205] in the upper, lower, and left directions; // indicates whether the request has passed through the struct node // priority queue, the first {int X, Y; int num; friend bool operator <(node A, Node B) {return. num> B. num ;}; int BFS (int n, int M, int startx, int starty) // BFS searches for the Start Node, and then checks whether the Start Node can be added to the queue, if yes, enter {priority_queue <node> Q; struct node, mark; node. X = startx; node. y = starty; node. num = 0; visited [startx] [starty] = 1; q. push (node); While (! Q. empty () {node = Q. top (); q. pop (); For (INT I = 0; I <4; I ++) {mark = node; mark. X + = dir [I] [0]; mark. Y + = dir [I] [1]; If (mark. x> = 1 & mark. x <= N & mark. y> = 1 & mark. Y <= M &&! Visited [Mark. x] [Mark. y]) {visited [Mark. x] [Mark. y] = 1; if (Map [Mark. x] [Mark. y] = 'A') // returns {mark if the end point is found first. num ++; return mark. num;} else if (Map [Mark. x] [Mark. y] = '. ') {mark. num ++; q. push (Mark);} else if (Map [Mark. x] [Mark. y] = 'X') {mark. num + = 2; q. push (Mark) ;}}}return maxn;} int main () {int n, m, s; int startx, starty; while (scanf ("% d", & N, & M )! = EOF) {for (INT I = 1; I <= N; I ++) {getchar (); For (Int J = 1; j <= m; j ++) {visited [I] [J] = 0; scanf ("% C", & map [I] [J]); if (Map [I] [J] = 'R') {startx = I; starty = J ;}} S = BFS (n, m, startx, starty ); if (s! = Maxn) printf ("% d \ n", S); else printf ("Poor Angel has to stay in the prison all his life. \ n ") ;}return 0 ;}