# Include <stdio. h> # include <string. h ># include <queue> using namespace STD; struct node {int X, Y, step ;}; char map [105] [105]; int vis [105] [105]; int to [4] [2] = {,-, 0,-1}; int n, m, Sx, sy, ex, ey, ans; int check (int x, int y) {If (x <0 | x> = n | Y <0 | Y> = m) return 1; if (vis [x] [Y] | map [x] [Y] = '#') return 1; return 0;} void BFS () {int I; queue <node> q; node A, next;. X = SX;. y = sy;. step = 0; vis [. x] [A. Y] = 1; q. Push (a); While (! Q. empty () {A = Q. front (); q. pop (); If (Map [. x] [. y] = 'E') {ans =. step; return ;}for (I = 0; I <4; I ++) {next = A; next. X + = to [I] [0]; next. Y + = to [I] [1]; If (check (next. x, next. y) continue; next. step =. step + 1; vis [next. x] [next. y] = 1; q. push (next) ;}}ans =-1 ;}int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); int I, j; for (I = 0; I <n; I ++) scanf ("% s", map [I]); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] = 's') {SX = I; Sy = J ;}} memset (VIS, 0, sizeof (VIS); BFS (); printf ("% d \ n", ANS);} return 0 ;}
Template, BFs