A pure water question... directly deep search and memorizing and marking
# Include <stdio. h> # include <stdlib. h> char map [1005] [1005]; int des [1005] [1005], M, N, S, F1, F2, INF = 0x7fffffff, dep [1005] [1005], S1, S2, S3; void DFS (INT y, int X, int step) {If (des [y] [x]) // If the loop {F2 = 1; S2 = step; S3 = Dep [y] [X]; return ;} if (F1 | map [y] [x] <0) // If the boundary {F1 = 1; S1 = step; return ;} des [y] [x] = 1; Dep [y] [x] = step; If (Map [y] [x] = 'n ') DFS (Y-1, x, step + 1); else if (Map [y] [x] = 's') DFS (Y + 1, x, step + 1); else if (Map [y] [x] = 'E') DFS (Y, x + 1, step + 1); else DFS (Y, x-1, step + 1); return;} int main () {While (scanf ("% d", & N, & M), M | N) {scanf ("% d % * C", & S); F1 = F2 = 0; For (INT I = 0; I <1005; ++ I) // initialization you know for (Int J = 0; j <1005; ++ J) Dep [I] [J] = des [I] [J] = 0, map [I] [J] =-1; for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) scanf ("% C", & map [I] [J]); getchar ();} DFS (1, S, 0); If (F1) printf ("% d step (s) to exit \ n", S1); else printf ("% d step (s) before a loop of % d step (s) \ n ", S3, s2-s3);} return 0 ;}