Maze of DFS

Question: Input n, m denotes n rows and m columns the size of the maze, after the value n input lines maze, represents the road, '*' represents a wall, 's' represents a starting point, 'T' indicates the end find a labyrinth '.' solution and labeled with a character line m.

#include <the iostream>
 the using  namespace STD;
 int n-, m; // size of 
char Maze [ 105 ] [ 105 ]; // value stored maze 
BOOL VIS [ 105 ] [ 105 ];   // to store this point is to go over 
int the dir [ . 4 ] [ 2 ] = {{- . 1 , 0 }, { 0 , - . 1 }, { . 1 , 0 }, { 0 , . 1 }};   // four on the lower left and right directions are counterclockwise 
BOOL  in (int X, int Y) {
     IF (X> = 0 && X <n-&& Y> = 0 && Y < m) {
         return  to true ; 
    } 
    return  to false ; 
} 
// in function used to determine whether the bounds 
BOOL DFS ( int X, int Y) {
     IF (Maze [X] [Y] == ' T ' ) {
         return  to true ; 
    } // find direct return to true 
    IF ( in (X, Y) && VIS [X] [Y] && Maze [X] [Y! ]! = ' * ' ) {
        am [x] [y]= To true ; 
        Maze [X] [Y] = ' m ' ; // points that satisfy the conditions, through the mark, Mark the m 
        for ( int I = 0 ; I < . 4 ; I ++ ) {
             IF (DFS (X + the dir [I] [ 0 ], Y + the dir [I] [ . 1 ])) {
                 return  to true ; 
            } 
        } // four directions search An exhaustive enumeration method 
        VIS [X] [Y] = to false ; // If found early will return true; come this far it means the search down point is not found, then the cancellation point mark, the mark m remove 
        Maze [the X-] [the y-] = ' .' ;
        return  to false ; // returns to false 
    } 
} 
int main () {
     int X, Y; // used to mark the starting point coordinate 
    CIN >> >> n- m;
     for ( int I = 0 ; I <n-; I ++ ) { 
        CIN >> maze [I]; // the value of the input entire maze 
    }
     for ( int I = 0 ; I <= n-; I ++ ) {
         for ( int J = 0 ; J <= m; J ++ ) {
             IF (maze [I] [J] == ' S ' ) { 
                X = I; 
                Y = J; // find the coordinates of the starting point of the maze 
            } 
        } 
    } 
    DFS (X, Y); // find maze solution 
    for ( int I = 0 ; I <n-; I ++ ) { 
        COUT << maze [I] << endl; // input labeled Solutions with labyrinth 
    }
     return  0 ; 
}

dfs here uses a recursive thinking, the equivalent of four directions four times recursive, recursion four directions, the figure represents the starting point has been up to go black, go black after, then left to go black the figure is used in counterclockwise order, when up went the second point from the first point, dfs second point is the same as the first and determine whether the find first end, and if not then take as a point of strategy, first come up black, black and then go left a counterclockwise direction, this recurrence, to the last point, four times dfs last point is return false because nowhere are four directions, the code execution of the last point to the unmarked vis, m cancellation of the mark point, because there is no return true the last point of the penultimate point so no return true, the penultimate dfs points down to the code to thereby perform, VIS Unmark, m cancellation of markers, in order to go back to the first point, the first case dfs a first direction and a point can not go through into the second dfs direction, with the above-described process wherein Like, until you find the end, return true, and vice versa return false.

 

Guess you like

Origin www.cnblogs.com/jcahsy/p/12451759.html