Enter three integers, n, m, and k, indicating that there is a map with n rows and m columns next. A robot enters from column k of the first line. Ask how many steps the robot can take. If a loop occurs
The number of steps in the output loop.
Solution: DFS
The Code is as follows (with a detailed explanation ):
* 1035_1.cpp ** Created on: July 15, August 17, 2013 * Author: Administrator * // * simple search question. See the notes: */# include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cmath> # include <algorithm> using namespace std; /*** map [] []: used to mark the map * num [x] [y]: used to mark the steps used to reach the (x, y) Point. * If num [x] [y] = 0, it indicates that this step has not passed. Otherwise, a loop ** n: number of rows * m: Number of columns * k: Enter the map from column k of the first row * t: total steps * lop: number of cycles */char map [11] [11]; int num [11] [11]; int n, m, k, lop, t;/*** determine whether to cross-border */bool Overmap (int x, int y) {if (x <1 | x> n | y <1 | y> m) {return true;} else {return false ;}} void Dfs (int x, int y) {// if it is out of bounds or if (Overmap (x, y) | lop> 0) {if (lop> 0) {printf ("% d step (s) before a loop of % d step (s) \ n ", T-lop, lop);} else {printf ("% d step (s) to exit \ n", t);} return ;} if (num [x] [y] = 0) {// if this step has not passed num [x] [y] = ++ t ;} else {// determine whether a loop exists (if this step has passed, calculate the number of cycles) lop = t-num [x] [y] + 1 ;} // else // traversal status switch (map [x] [y]) {case 'N': x --; Dfs (x, y); x ++; break; // Dfs: the first condition to be added. The following conditions must be returned: case 'E': y ++; Dfs (x, y); y --; break; case's ': x ++; Dfs (x, y); x --; break; case 'W': y --; Dfs (x, y); y ++; break ;} // sw Itch} // dfs int main () {while (scanf ("% d", & n, & m, & k )! = EOF, n) {memset (map, 0, sizeof (map); memset (num, 0, sizeof (num); lop = 0; t = 0; getchar (); for (int I = 1; I <= n; I ++) {scanf ("% s", map [I] + 1 ); getchar () ;}dfs (1, k) ;}/ ** 1035_1.cpp ** Created on: July 15, August 17, 2013 * Author: Administrator * // * simple search question, see note: */# include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cmath> # include <algorithm> using namespace std; /*** map [] []: Use To mark the map * num [x] [y]: To mark how many steps are used to reach the (x, y) Point. * If num [x] [y] = 0, it indicates that this step has not passed. Otherwise, a loop ** n: number of rows * m: Number of columns * k: Enter the map from column k of the first row * t: total steps * lop: number of cycles */char map [11] [11]; int num [11] [11]; int n, m, k, lop, t;/*** determine whether to cross-border */bool Overmap (int x, int y) {if (x <1 | x> n | y <1 | y> m) {return true;} else {return false ;}} void Dfs (int x, int y) {// if it is out of bounds or if (Overmap (x, y) | lop> 0) {if (lop> 0) {printf ("% d step (s) before a loop of % d step (s) \ n", t-lop, Lop);} else {printf ("% d step (s) to exit \ n", t);} return ;} if (num [x] [y] = 0) {// if this step has not passed num [x] [y] = ++ t ;} else {// determine whether a loop exists (if this step has passed, calculate the number of cycles) lop = t-num [x] [y] + 1 ;} // else // traversal status switch (map [x] [y]) {case 'N': x --; Dfs (x, y); x ++; break; // Dfs: the first condition to be added. The following conditions must be returned: case 'E': y ++; Dfs (x, y); y --; break; case's ': x ++; Dfs (x, y); x --; break; case 'W': y --; Dfs (x, y); y ++; break ;} // switch} // d Fsint main () {while (scanf ("% d", & n, & m, & k )! = EOF, n) {memset (map, 0, sizeof (map); memset (num, 0, sizeof (num); lop = 0; t = 0; getchar (); for (int I = 1; I <= n; I ++) {scanf ("% s", map [I] + 1 ); getchar () ;}dfs (1, k );}}