回溯法-(牛客网)矩阵中的路径

链接:https://www.nowcoder.com/questionTerminal/c61c6999eecb4b8f88a98f66b273a3cc
来源:牛客网

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

DFS + 回溯

class Solution {
public:
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        if(matrix==NULL || rows<1 || cols<1 || str == NULL)
            return false;
        //bool *visited = new bool[rows*cols];
        bool visited[rows*cols];
        memset(visited, 0, rows*cols);
        int pathlen = 0;  //记录路径长度
        for(int i=0; i<rows; i++){
            for(int j=0; j<cols; j++){
                if(dfs(matrix, rows, cols, str, i, j, pathlen, visited))
                    return true;
            }
        }
        //delete[] visited;
        return false;
    }

    bool dfs(char* matrix, int rows, int cols, char* str, int row, int col, int& pathlen, bool* visited)
    {
        if(str[pathlen] == '')
            return true;  //遍历到字符串的末尾了,说明在矩阵中能找到str
        bool hasPath = false;
        if(row>=0 && row<=rows && col>=0 && col<=cols && matrix[row*cols+col] == str[pathlen] && !visited[row*cols+col]){
            //找到了一个符合条件的字符
            pathlen++;
            visited[row*cols+col] = true;
            hasPath = dfs(matrix, rows, cols, str, row-1, col, pathlen, visited) || dfs(matrix, rows, cols, str, row+1, col, pathlen, visited) 
                || dfs(matrix, rows, cols, str, row, col-1, pathlen, visited) || dfs(matrix, rows, cols, str, row, col+1, pathlen, visited);
            
            if(!hasPath){
                //对于下标为pathlen的字符,若相邻格子都没找到匹配str中下标为pathlen+1的字符,则回溯到pathlen-1中
                pathlen--;
                visited[row*cols+col] = false;
            }
        }
        return hasPath;
    }
};

机器人的运动范围 

链接:https://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8
来源:牛客网

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

class Solution {
public:
    int movingCount(int threshold, int rows, int cols)
    {
        if(threshold<0 || rows<0 || cols<0 )
            return false;
        bool visited[rows*cols];
        memset(visited, 0, rows*cols);
        int res = dfs(threshold, rows, cols, 0, 0, visited);
        return res;
    }
    
    int dfs(int th, int rows, int cols, int row, int col, bool* visited){
        int count = 0;
        if(check(th, rows, cols, row, col, visited)){
            visited[row*cols+col] = true;
            count = 1 + dfs(th, rows, cols, row+1, col, visited) + dfs(th, rows, cols, row-1, col, visited) + dfs(th, rows, cols, row, col-1, visited) + dfs(th, rows, cols, row, col+1, visited);
        }
        return count;
    }
    
    bool check(int th, int rows, int cols, int row, int col, bool* visited){
        if(row>=0 && row<rows && col>=0 && col<cols && !visited[row*cols+col] && getSum(row)+getSum(col)<=th)
            return true;
        return false;
    }
    
    int getSum(int num){
        int sum = 0;
        while(num){
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }
};
原文地址:https://www.cnblogs.com/Bella2017/p/11815227.html