Prove safety Offer: path matrix (java version)

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 one path through the matrix of one grid, the following can not enter the grid again. Such abcesfcsadee e.g. 3 X 4 matrix containing the path "bcced" in a string, but does not contain the matrix "abcb" path, since the first character string occupies the first row of the matrix b in the second lattice after that, the path can not enter the grid again.

analysis

First find str [0] is located at position i matrix [], and then start from the position i, up, down, left, or right to look to see whether a match under str, if matched, will matrix a position corresponding to '-' indicates that after the grid, can not enter back again. Then repeat up, down, left, or right to find a process, until a match is completed str, returns true.
The matching process, if it is found out of range i, the current position is not matched, the location is already i '-', it returns false.
There may exist a plurality of STR matrix [] [0], not possible with this beginning, but the beginning of another are possible. It is necessary to find the next str [0] in the Matrix [], and then repeat the above matching process. (Note here that, to the matrix '-' symbol reduction)

public class Solution {
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
        char[] ch = copy(matrix); //先copy一个matrix,方便后面还原
        for(int i=0; i<matrix.length; i++){
            if(matrix[i] == str[0]){ //如果能找到str[0]所在的位置i
                if(match(matrix, i, rows, cols, str , 0)){ //如果能完全匹配,返回true
                    return true;
                }else{ // 上面的匹配失败,还原matrix,再去找下一个开头
                    matrix = copy(ch);
                }
            }
        }
        return false;
    }
    boolean match(char[] matrix, int i, int rows, int cols, char[] str, int j){
        if(j==str.length) //如果str匹配完了,返回true
            return true;
        if(i<0 || i>matrix.length-1 || matrix[i]!=str[j] || matrix[i]=='-') //如果越界、匹配不上、i位置已经匹配过了
            return false;
        matrix[i]='-'; //没有越界并且匹配上了,就将当前位置设为‘-’
        if(match(matrix, i-1, rows, cols, str, j+1) || //去匹配上、下、左、右的位置
           match(matrix, i+1, rows, cols, str, j+1) ||
           match(matrix, i+cols, rows, cols, str, j+1)||
           match(matrix, i-cols, rows, cols, str, j+1)){
            return true;
        }
        return false;
    }
    char[] copy(char[] matrix){
        char[] ch = new char[matrix.length];
        for(int i=0;i<ch.length; i++){
            ch[i]=matrix[i];
        }
        return ch;
    }
}

Matrix using a boolean flag visited location

public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
        boolean[] visited = new boolean[matrix.length];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (searchFromHere(matrix,rows,cols,i,j,0,str,visited))
                    return true;
            }
        }
        return false;
    }
    public boolean searchFromHere(char[] matrix,int rows,int cols,int r,int c,int index,char[] str,boolean[] visited){
        if (r < 0 || r >= rows || c < 0 || c >= cols || matrix[r * cols + c] != str[index] || visited[r * cols + c])
            return false;
        if (index == str.length - 1)    return true;
        visited[r * cols + c] = true;
        if (searchFromHere(matrix,rows,cols,r - 1,c,index + 1,str,visited) ||
                searchFromHere(matrix,rows,cols,r,c -1,index + 1,str,visited) ||
                searchFromHere(matrix,rows,cols,r + 1,c,index + 1,str,visited) ||
                searchFromHere(matrix,rows,cols,r,c + 1,index + 1,str,visited))
            return true;
        visited[r * cols + c] = false;
        return false;
    }
}

Guess you like

Origin blog.csdn.net/qq_43165002/article/details/90696265