A very simple question, step by step. Going out of the matrix indicates that there is no ring. If you go to a place that has already passed, it indicates that there is a ring. You can output the result in the format !!!
# Include <stdio. h> # include <string. h> int n, m, temp; int ans [1010] [1010]; char map [1010] [1010]; void dfs (int sx, int sy) {while (sx> = 0 & sx <n & sy> = 0 & sy <m & map [sx] [sy]! = 'O') {if (map [sx] [sy] = 's') {// temp ++; map [sx] [sy] = 'O '; ans [sx] [sy] = ++ temp; sx ++;} else if (map [sx] [sy] = 'n') {// temp ++; map [sx] [sy] = 'O'; ans [sx] [sy] = ++ temp; sx --;} else if (map [sx] [sy] = 'E') {// temp ++; map [sx] [sy] = 'O '; ans [sx] [sy] = ++ temp; sy ++;} else if (map [sx] [sy] = 'W') {// temp ++; map [sx] [sy] = 'O'; ans [sx] [sy] = ++ temp; sy --;}} if (map [sx] [sy] = 'O') printf ("% d step (s) before a loop of % d step (s) \ n ", ans [sx] [sy]-1, temp-ans [sx] [sy] + 1); elseprintf ("% d step (s) to exit \ n ", temp) ;}int main () {int k, I; while (scanf ("% d", & n, & m, & k ), n + m + k) {memset (ans, 0, sizeof (ans); for (I = 0; I <n; I ++) scanf ("% s ", map [I]); temp = 0; dfs (0, k-1);} return 0 ;}