Depth-first search - Maze

Valley from Los P1605

Title Description

Given an N * M squares maze, T-maze at the barrier, not through the barrier. Given the start point and end point, Q: After a maximum of 1 per box, how many from the start point to the end point coordinate coordinates the program. In the maze to move up and down about four ways, you can only move one square. No obstacles to ensure data starting point.

Input Format

The first row N, M and T, N row, M being a column, T is the total number of disorders. The second line starting coordinates SX, SY, ending coordinate FX, FY. Then the coordinates of the point T lines, each behavioral disorders.

Output Format

Given the start point and end point, and asked after each square up to 1 times the total number of programs from the starting point to the end point coordinate coordinates.

Sample input and output

Input 1:

2 2 1
1 1 2 2
1 2

  

Output 1:

1

    

Input 2:

5 5 0
1 1 2 2

Output 2:

4846

  

 

code show as below:

 

#include <the iostream>
 the using  namespace STD;
 int n-, m, T, SX, SY, FX, FY;
 const  int N = 10 ; // N is not less than. 6 
int  IS [N] [N]; // go by 0 road and obstacles exist, the default global variables are 0 
int SUM = 0 ;
 void DFS ( int X, int Y) {
     IF (X == Y == && FX FY) { 
        SUM ++ ;
         return ; 
    } 
    the else {
         iS [ X] [Y] = 0 ; //The current path to walk (the next will explore the way this is set to come) 
         // four directions exploration and backtracking 
        IF ( IS [the X-] [Y- 1 ]) {the DFS (the X-, Y- 1 ); IS [X] [Y- . 1 ] = . 1 ;} 
         IF ( IS [X] [Y + . 1 ]) {DFS (X, Y + . 1 ); IS [X] [Y + . 1 ] = . 1 ;}
         IF ( IS [X + . 1 ] [Y]) {DFS (X + . 1 , Y); IS [X + . 1 ] [Y] = . 1 ;}
         IF ( IS [X- . 1] [Y]) {DFS (X- . 1 , Y); IS [X- . 1 ] [Y] = . 1 ;} 
        } 
    
} 
int main () {     

    int X, Y, I, J; 
    CIN >> >> n- >> m T; 
    CIN >> SX SY >> >> >> FX FY; 

    for (I = . 1 ; I <= n-; I ++ ) 
         for (J = . 1 ; J <= m; J ++ )
         IS [I] [ J] = . 1 ; // the first labyrinth range are set to go, or no border of 


    for (I = . 1 ; I <= T; I ++ ) { 
        CIN >> >> X Y;
         IS[X] [Y] = 0 ; // disorder marked not go 
    } 
    DFS (SX, SY); 
    COUT << SUM;
     return  0 ; 
}

 

 

 

  

 

 

Guess you like

Origin www.cnblogs.com/wangyafeidr/p/12540082.html