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