Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242 Rescue
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 15597 accepted submission (s): 5663
Problem description 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
When I first saw this question, I thought it would be fine to use the template. It was not difficult, but I had been wrong when I typed out the code and submitted it. I really couldn't figure it out. I thought I was such a grumpy person, it's just getting mad. AC code (1): It's lucky to use a wide search template.
# Include <iostream> # include <cstring> # include <cstdlib> # include <queue> using namespace STD; # define Max 201 char a [Max] [Max]; int move [4] [2] = {-, 0,-1}, V [Max] [Max]; // It is important to arrange the movement direction here. Int n, m, ans; struct node {int X, Y, step ;}; void BFS (int I, Int J) {queue <node> q; node now, next; now. X = I; now. y = J; V [now. x] [now. y] = 1; now. step = 0; q. push (now); While (! Q. empty () {now = Q. front (); q. pop (); if (a [now. x] [now. y] = 'R') {ans = now. step; return;} else {for (int t = 0; t <4; t ++) {next. X = now. X + move [T] [0]; next. y = now. Y + move [T] [1]; If (! V [next. x] [next. Y] & A [next. x] [next. Y]! = '#' & Next. x> = 0 & next. x <n & next. y> = 0 & next. Y <m) {v [next. x] [next. y] = 1; if (a [next. x] [next. y] = '. '| A [next. x] [next. y] = 'R') Next. step = now. step + 1; else if (a [next. x] [next. y] = 'X') Next. step = now. step + 2; q. push (next) ;}}}return;} int main () {int I, j; while (CIN >>n> m) {ans = 0; memset (v, 0, sizeof (v); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {CIN> A [I] [J] ;}} for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {if (a [I] [J] = 'A') BFS (I, j) ;}} if (ANS) cout <ans <Endl; else cout <"Poor Angel has to stay in the prison all his life. \ n ";}return 0 ;}
AC code (2): it is more reliable than the previous one by using the priority queue.
# Include <iostream> # include <cstring> # include <queue> using namespace STD; # define M 205 char C [m] [m]; int V [m] [m]; int W [4] [2] = {-, 0,-, 0}; int ans, n, m; struct node {int X, Y, time; friend bool operator <(const node A, const Node B) {return. time> B. time ;}}; void BFS (int A, int B) {node now, TMP; priority_queue <node> q; now. time = 0; now. X = A; now. y = B; memset (v, 0, sizeof (v); V [a] [B] = 1; q. push (now); While (! Q. empty () {now = Q. top (); q. pop (); If (C [now. x] [now. y] = 'R') {ans = now. time; return ;}for (INT I = 0; I <4; I ++) {TMP. X = now. X + W [I] [0]; TMP. y = now. Y + W [I] [1]; If (TMP. x> = 0 & TMP. x <n & TMP. y> = 0 & TMP. Y <M &&! V [TMP. x] [TMP. Y] & C [TMP. x] [TMP. Y]! = '#') {V [TMP. x] [TMP. y] = 1; if (C [TMP. x] [TMP. y] = 'R' | C [TMP. x] [TMP. y] = '. ') TMP. time = now. time + 1; if (C [TMP. x] [TMP. y] = 'X') TMP. time = now. time + 2; q. push (TMP) ;}}return ;}int main () {While (CIN >>n> m) {int I, j, X, Y; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {CIN> C [I] [J]; if (C [I] [J] = 'A') x = I, y = J;} ans = 0; BFS (x, y); If (ANS) cout <ans <Endl; elsecout <"Poor Angel has to stay in the prison all his life. \ n ";}return 0 ;}