Prove safety offer: 68 matrix path (DFS backtracking recursive maze-like)

1. Title Description

/ * 
    Please design a function that determines whether there is a route that contains all the characters of a character string in a matrix. 
    A lattice path may start from any of the matrix, each step in the matrix can be left, right, up, down one grid. 
    If a route that passes through a matrix of a grid, the path can not enter the lattice. 

For chestnut: 
    for example include path "bcced" is a character string in the matrix below 3 * 4. 
  However, the matrix does not contain the path string "abcb", since the first character of the string b occupy the first row of the matrix after a second grid, the grid can not enter the path again. ABCE of SFCs after stimulation Adee
* /

 

2. Code

Ideas, backtracking DFS

Import Classes in java.util *. ;
 public  class Solution {
     public  static  char [] the Matrix;
     public  static  int rows;
     public  static  int cols;
     public  static  char [] str;
     public  static  boolean [] Flag; // flag has entered the 
    public  Boolean hasPath ( char [] Matrix, int rows, int cols, char [] STR) {
         // flag is initialized to false 
        the this .matrix =Matrix;
         the this .Rows = rows;
         the this .cols = cols;
         the this .str = STR; 
        In Flag = new new  Boolean [matrix.length];
         for ( int I = 0; I <rows; I ++ ) {
             for ( int J = 0 ; J <cols; J ++ ) {
                  // loop through the two-dimensional array to find the starting point is equal to a value of the first element str, recursively determines whether surrounded ---- backtracking qualifying 
                 iF (DFS (I, J, 0 )) {
                      return  to true ; 
                 } 
            } 
        } 
        return to false ; 
    } 
     
    // DFS (row coordinate index i, the index ordinate j, the index of the string to be determined K) 
    Private  Boolean DFS ( int i, int j, int K) {
         // first calculated according to i and j matches a first one-dimensional array element into position 
        int index * cols = I + J;
         // recursive termination condition 
        IF (I <0 || I> J = rows || <|| J 0> cols = || in Flag [ ! index] || Matrix [index] = str [k]) {
              return  to false ; 
        } 
        // If k has reached the end of str, the foregoing description have been successfully matched, the direct return to true 
        IF (k == str . 1-.length ) {
              return  to true ; 
        }
        // go first position is set to true, indicating that has gone 
        In Flag [index] = to true ; 
         
        // backtracking recursion to find, k is incremented each time a give found, not found, reducing 
        IF (DFS (i-1, j, k + 1) || dfs (i + 1, j, k + 1) || dfs (i, j-1, k + 1) || dfs (i, j + 1, k + 1'd )) {
             return  to true ; 
        } 
        // get that described this road barrier, reducing, try other paths 
        in Flag [index] = to false ;
         return  to false ; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/haimishasha/p/11519964.html