Maze problem 1 find a path of the maze (DFS + backtracking)

Source: Internet
Author: User

Maze problem 1 find a path of the maze (DFS + backtracking)

Problem description:

One day, james accidentally entered a maze. Now, please help him determine whether he can leave the maze. If possible, he will output YES. if you cannot exit, NO is output. each walk can only be up to four directions.

* Indicates that it is accessible.

# Indicate obstacles

T indicates exit

The entry is (), and the data guarantee is the entry in the upper left corner.


# Include
 
  
Using namespace std; char maze [100] [100]; bool flag [100] [100]; int dx [] = {, 1,-1 }; int dy [] = {1,-1, 0}; int m, n; bool dfs (int x, int y) {flag [x] [y] = 1; // mark the road sign as 1if (maze [x] [y] = 'T') return true; for (int I = 0; I <4; I ++) // Four Directions {int nx = x + dx [I]; int ny = y + dy [I]; if (flag [nx] [ny] = 0 | maze [nx] [ny] = '*' | maze [nx] [ny] = 'T' & nx> 0 & ny> 0 & nx
  
   
> M> n) {memset (maze, 0, sizeof (maze); memset (flag, 0, sizeof (flag); for (int I = 1; I <= m; I ++) for (int j = 1; j <= n; j ++) cin> maze [I] [j]; if (dfs (1, 1) cout <"YES" <
   
    
Test data:
    

3 3

*##

***

# T

4

****

*##*

**#*

### T


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.