(Step 4.3.5) hdu 1035 (Robot Motion -- DFS)

Source: Internet
Author: User

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 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.