12 wins the face questions offer a path matrix

Question: designing a function for determining 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 example the matrix containing the path "bcced" in a string, but does not contain the matrix "abcb" path, as a character string in the first row of the matrix b occupy the first second after the lattice, can not re-enter the path this grid.

Add their own interpretation of the subject: string path order to be consistent

Input: char * matrix, int rows, int cols, char * str

Output: whether there is a path

Ideas: backtracking (the path as a stack)

Open the same size of the matrix bool matrix to determine whether the path into the grid.

Optionally a lattice path as a starting point,

If the current node is consistent with the i-th character is around the lattice match whether there is a character i + 1-th lattice matching, the absence of a grid-oriented backtracking.

If the current node is inconsistent with the i-th character, then fall back to the first i-1 characters continue to compare not entered into the grid.

Code:

#include <string.h>
class Solution {
public:
    bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, const char* str, int& pathLength, bool* visited)
    {
        if(str[pathLength]=='\0')
            return true;
        bool hasPath=false;
        // explore neighborhood
        if(row>=0&&row<rows&&col>=0&&col<cols&&matrix[row*cols+col]==str[pathLength]&&!visited[row*cols+col])
        {
            ++pathLength;
            visited[row*cols+col]=true;
            hasPath=hasPathCore(matrix,rows,cols,row+1,col,str,pathLength,visited)||
                hasPathCore(matrix,rows,cols,row-1,col,str,pathLength,visited)||
                hasPathCore(matrix,rows,cols,row,col+1,str,pathLength,visited)||
                hasPathCore(matrix,rows,cols,row,col-1,str,pathLength,visited);
            if(!hasPath)  //Backtracking  注意:这段代码的位置在if的括号内!
            {
                --pathLength;
                visited[row*cols+col]=false;
            }
        }
        return hasPath;
    }
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        if(matrix==nullptr||rows<=0||cols<=0||str==nullptr)
            return false;
        bool* visited=new bool[rows*cols];
        // initialize visited
        memset(visited,0,rows*cols);
        int pathLength=0;
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                if(hasPathCore(matrix,rows,cols,i,j,str,pathLength,visited))
                    return true;
            }
        }
        // delete visited
        delete[] visited;
        return false;
    }


};

Complexity analysis: time complexity O(n^2*2^m), space complexity O(n^2+m).

 
Published 56 original articles · won praise 10 · views 6797

Guess you like

Origin blog.csdn.net/qq_22148493/article/details/104431628
Recommended