Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242
The only difference in BFS is that point X can be stuck.
The method adopted this time is different from the previous method for processing X points.
Note vis [] [] records whether to access and the shortest time from this point
# Include <stdio. h> # include <string. h> # include <stdlib. h> char map [205] [205]; int vis [205] [205]; struct node {int X, Y; int s;} DUI [80005]; int dir [4] [2] = {, 0,-, 0,-}; int n, m, s_x, s_y; void BFS () {int I, j; int head, tail; struct node next, now; memset (VIS, 0, sizeof (VIS); Head = tail = 0; DUI [tail]. X = s_x; DUI [tail]. y = s_y; DUI [tail ++]. S = 1; vis [s_x] [s_y] = 1; while (Head <tail) {now. X = DUI [head]. x; now. y = DUI [head]. Y; Now. S = DUI [head ++]. s; If (Map [now. x] [now. y] = 'X') {map [now. x] [now. y] = '. '; DUI [tail]. X = now. x; DUI [tail]. y = now. y; DUI [tail ++]. S = now. S + 1; continue;} for (I = 0; I <4; I ++) {next. X = now. X + dir [I] [0]; next. y = now. Y + dir [I] [1]; next. S = now. S + 1; if (next. x <0 | next. Y <0 | next. x> = n | next. y> = M | map [next. x] [next. y] = '#') continue; If (vis [next. x] [next. y]! = 0) continue; vis [next. x] [next. y] = next. s; DUI [tail]. X = next. x; DUI [tail]. y = next. y; DUI [tail ++]. S = next. s ;}} int main () {int I, j, ans; while (scanf ("% d", & N, & M )! = EOF) {ans = 10000000; getchar (); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {scanf ("% C", & map [I] [J]); If (Map [I] [J] = 'A ') {s_x = I; s_y = J ;}} getchar () ;}bfs (); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] = 'R ') {If (vis [I] [J] = 0) continue; else {If (ANS> vis [I] [J]) ans = vis [I] [J] ;}}} if (ANS = 10000000) printf ("Poor Angel has to stay in the prison all his life. \ n "); else printf (" % d \ n ", ans-1);} return 0 ;}