Link: poj 2251
The question is extended from two-dimensional space to three-dimensional space. It can be moved up, down, left, right, and back, and can only be moved to adjacent spaces each time,
It takes at least one minute to start from the start point to the end point.
# Indicates the rock, space, s indicates the starting point, and E indicates the ending point.
This question only needs to be performed in six BFS directions, and has timed out since then. Change it to DFS for WA,
It is found that after each access, it should be immediately marked as accessed, And I marked as accessed by converting the accessed point into #, that is, the rock.
# Include <cstdio >#include <cstring> # include <queue> using namespace STD; struct Stu {int I, j, k ;}; char s [35] [35] [35]; int M, N, P; int BFS (struct Stu a, struct Stu B) {int I, J, K, T [35] [35] [35] = {0}; queue <struct Stu> q; q. push (a); s [. i] [. j] [. k] = '#'; while (! Q. empty () {A = Q. front (); q. pop (); I =. i; j =. j; k =. k; if (I = B. I & J = B. J & K = B. k) // return t after the end is reached [I] [J] [k]; if (I> 0 & S [I-1] [J] [k]! = '#') {. I = I-1;. j = J;. k = K; q. push (a); t [I-1] [J] [k] = T [I] [J] [k] + 1; // calculate the time s [I-1] [J] [k] = '#'; // remember to mark it immediately after access} if (I <M-1 & S [I + 1] [J] [k]! = '#') {. I = I + 1;. j = J;. k = K; q. push (a); t [I + 1] [J] [k] = T [I] [J] [k] + 1; s [I + 1] [J] [k] = '#';} If (j> 0 & S [I] [J-1] [k]! = '#') {. I = I;. j = J-1;. k = K; q. push (a); t [I] [J-1] [k] = T [I] [J] [k] + 1; s [I] [J-1] [k] = '#';} If (j <n-1 & S [I] [J + 1] [k]! = '#') {. I = I;. j = J + 1;. k = K; q. push (a); t [I] [J + 1] [k] = T [I] [J] [k] + 1; s [I] [J + 1] [k] = '#';} If (k> 0 & S [I] [J] [k-1]! = '#') {. I = I;. j = J;. k = K-1; q. push (a); t [I] [J] [k-1] = T [I] [J] [k] + 1; s [I] [J] [k-1] = '#';} If (k <P-1 & S [I] [J] [k + 1]! = '#') {. I = I;. j = J;. k = k + 1; q. push (a); t [I] [J] [k + 1] = T [I] [J] [k] + 1; s [I] [J] [k + 1] = '#';} return-1; // if it is not possible to reach the end point, return-1} int main () {int I, J, K, sum; struct Stu X, Y; while (scanf ("% d", & M, & N, & P )! = EOF) {getchar (); If (M = 0 & n = 0 & P = 0) break; for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {for (k = 0; k <p; k ++) {scanf ("% C", & S [I] [J] [k]); If (s [I] [J] [k] ='s ') {X. I = I; X. j = J; X. k = K;} else if (s [I] [J] [k] = 'E') {Y. I = I; Y. j = J; Y. k = K ;}} getchar () ;}getchar () ;}sum = BFS (x, y); If (sum! =-1) printf ("escaped in % d minute (s). \ n", sum); else printf ("trapped! \ N ") ;}return 0 ;}