剑指offer算法题:机器人的运动范围 + 矩阵中的路径(回溯法)

机器人的运动范围

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

public class Solution {
    public int movingCount(int threshold, int rows, int cols)
    {
        if(threshold <= 0 || rows < 1 || cols < 1)
            return 0;
        boolean[] visited = new boolean[rows * cols]; //判断是否已经访问过的辅助数组
        return movingCount(threshold,rows,cols,0,0,visited);
    }
    private int movingCount(int threshold,int rows,int cols,int row,int col,boolean[] visited) {
        int count = 0;
        if(check(threshold,rows,cols,row,col,visited)) {
            visited[row * cols + col] = true;
            count = 1 + movingCount(threshold,rows,cols,row+1,col,visited)
                + movingCount(threshold,rows,cols,row,col+1,visited)
                + movingCount(threshold,rows,cols,row-1,col,visited)
                + movingCount(threshold,rows,cols,row,col-1,visited);
        }
        return count;
    }
    private boolean check(int threshold,int rows,int cols,int row,int col,boolean[] visited) {
        if(row >= 0 && row < rows 
           && col >= 0 && col < cols 
           && getDigitSum(row) + getDigitSum(col) <= threshold 
           && !visited[row * cols + col])
            return true;
        return false;
    }
    //判断数位之和是否小于k
    private int getDigitSum(int number) {
        int count = 0;
        while(number > 0) {
            count += number % 10;
            number /= 10;
        }
        return count;
    }
}

矩阵中的路径

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

public class Solution {
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
    {
        if(matrix == null || rows < 1 || cols < 1 || str == null)
            return false;
        boolean[] visited = new boolean[rows * cols];
        int pathLength = 0; //str数组中指定字符的指针
        //从矩阵的每一位开始判断
        for(int row = 0;row < rows;row++) {
            for(int col = 0;col < cols;col++) {
                if(hasPash(matrix,rows,cols,row,col,str,pathLength,visited))
                    return true;
            }
        }
        return false;
    }
    private boolean hasPash(char[] matrix,int rows,int cols,int row,int col,char[] str,int pathLength,boolean[] visited) {
        if(str.length == pathLength)return true; //递归退出条件,相等表示存在该路径
        boolean hasPath = false;
        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 = hasPash(matrix,rows,cols,row + 1,col,str,pathLength,visited)
                || hasPash(matrix,rows,cols,row,col + 1,str,pathLength,visited)
                || hasPash(matrix,rows,cols,row - 1,col,str,pathLength,visited)
                || hasPash(matrix,rows,cols,row,col - 1,str,pathLength,visited);
            //回溯
            if(!hasPath){
                pathLength--;
                visited[row * cols + col] = false;
            }
    }
        return hasPath;
    }
}
发布了239 篇原创文章 · 获赞 70 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43777983/article/details/104100076