牛客刷题回溯法之矩阵中的路径 and 机器人的运动范围

    科技2022-09-06  132

    题目描述一矩阵中的路径

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

    https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&&tqId=11218&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

    解题思路:

    分析:回溯算法 这是一个可以用回朔法解决的典型题。首先,在矩阵中任选一个格子作为路径的起点。如果路径上的第i个字符不是ch,那么这个格子不可能处在路径上的 第i个位置。如果路径上的第i个字符正好是ch,那么往相邻的格子寻找路径上的第i+1个字符。除在矩阵边界上的格子之外,其他格子都有4个相邻的格子。 重复这个过程直到路径上的所有字符都在矩阵中找到相应的位置。   由于回朔法的递归特性,路径可以被开成一个栈。当在矩阵中定位了路径中前n个字符的位置之后,在与第n个字符对应的格子的周围都没有找到第n+1个 字符,这个时候只要在路径上回到第n-1个字符,重新定位第n个字符。   由于路径不能重复进入矩阵的格子,还需要定义和字符矩阵大小一样的布尔值矩阵,用来标识路径是否已经进入每个格子。 当矩阵中坐标为(row,col)的 格子和路径字符串中相应的字符一样时,从4个相邻的格子(row,col-1),(row-1,col),(row,col+1)以及(row+1,col)中去定位路径字符串中下一个字符 如果4个相邻的格子都没有匹配字符串中下一个的字符,表明当前路径字符串中字符在矩阵中的定位不正确,我们需要回到前一个,然后重新定位。   一直重复这个过程,直到路径字符串上所有字符都在矩阵中找到合适的位置。 参考链接:

    https://www.nowcoder.com/questionTerminal/c61c6999eecb4b8f88a98f66b273a3cc

    代码:

    class Solution { public: bool hasPath(char* matrix, int rows, int cols, char* str) { if(rows==0 || cols==0 || matrix==NULL || str==NULL) return false; int pathLength = 0; vector<bool> visited(rows*cols, false); for(int row=0; row<rows; row++) { for(int col=0; col<cols; col++) { if(hasPathCore(matrix, str, rows, cols, row, col, pathLength, visited)) { return true; } } } return false; } bool hasPathCore(char* matrix, char* str, int rows, int cols, int row, int col, int& pathLength, vector<bool>& visited) { if(str[pathLength] == '\0') { return true; } bool hasPath = false; if(row>=0 && row<rows && col>=0 && col<cols && str[pathLength]==matrix[row*cols+col] && !visited[row*cols+col]) { pathLength++; visited[row*cols+col] = true; hasPath = hasPathCore(matrix, str, rows, cols, row-1, col, pathLength, visited) || hasPathCore(matrix, str, rows, cols, row, col-1, pathLength, visited) || hasPathCore(matrix, str, rows, cols, row+1, col, pathLength, visited) || hasPathCore(matrix, str, rows, cols, row, col+1, pathLength, visited); if(!hasPath) { pathLength--; visited[row*cols+col] = false; } } return hasPath; } };

    题目描述二 机器人的运动范围

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

    牛客链接:

    https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&&tqId=11219&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

    解题思路:

    核心思路: 1.从(0,0)开始走,每成功走一步标记当前位置为true,然后从当前位置往四个方向探索, 返回1 + 4 个方向的探索值之和。 2.探索时,判断当前节点是否可达的标准为: 1)当前节点在矩阵内; 2)当前节点未被访问过; 3)当前节点满足limit限制。 参考链接:

    https://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8

    代码:

    class Solution { public: int movingCount(int threshold, int rows, int cols) { if(rows<=0 || cols<=0 || threshold <=0) return 0; vector<bool> visited(rows*cols, false); int count = movingCountCore(threshold, rows, cols, 0, 0, visited); return count; } int movingCountCore(int threshold, int rows, int cols, int row, int col, vector<bool>& visited) { int count = 0; int sum = digitalSum(row) + digitalSum(col); if(row>=0 && col>=0 && row<rows && col<cols && sum<=threshold && !visited[row*cols+col]) { visited[row*cols+col] = true; count =1+ movingCountCore(threshold, rows, cols, row-1, col, visited) + movingCountCore(threshold, rows, cols, row, col-1, visited) + movingCountCore(threshold, rows, cols, row+1, col, visited) + movingCountCore(threshold, rows, cols, row, col+1, visited); } return count; } int digitalSum(int num) { int sum = 0; while(num>0) { sum = sum + num%10; num = num/10; } return sum; } };

    注意: 回溯法

    Processed: 0.008, SQL: 9