机器人的运动范围

地上有一个 m 行和 n 列的方格。一个机器人从坐标 (0, 0) 的格子开始移动,每一次只能向左右上下四个方向移动一格,可是不能进入行坐标和列坐标的数位之和大于 k 的格子。java

例如,当 k 为 18 时,机器人可以进入方格 (35,37),由于 3+5+3+7=18。可是,它不能进入方格 (35,38),由于 3+5+3+8=19。请问该机器人可以达到多少个格子?git

 

使用深度优先搜索(Depth First Search,DFS)方法进行求解。回溯是深度优先搜索的一种特例,它在一次搜索过程当中须要设置一些本次搜索过程的局部状态,并在本次搜索结束以后清除状态。而普通的深度优先搜索并不须要使用这些局部状态,虽然仍是有可能设置一些全局状态。数组

参考:https://blog.csdn.net/DERRANTCM/article/details/46887811.net

/**
     * 题目:地上有个m行n列的方格。一个机器人从坐标(0,0)的格子开始移动,
     * 它每一次能够向左、右、上、下移动一格,但不能进入行坐标和列坐标的数
     * 位之和大于k的格子。例如,当k为18时,机器人可以进入方格(35,37),
     * 由于3+5+3+7=18.但它不能进入方格(35,38),由于3+5+3+8=19.
     * 请问该机器人可以达到多少格子?
     *
     * @param threshold 约束值
     * @param rows      方格的行数
     * @param cols      方格的列数
     * @return 最多可走的方格
     */


public class Solution {
     public static int movingCount(int threshold, int rows, int cols) {
        // 参数校验
        if (threshold < 0 || rows < 1 || cols < 1) {
            return 0;
        }

        // 变量初始化
        boolean[] visited = new boolean[rows * cols];
        for (int i = 0; i < visited.length; i++) {
            visited[i] = false;
        }

        return movingCountCore(threshold, rows, cols, 0, 0, visited);
    }

    /**
     * 递归回溯方法
     *
     * @param threshold 约束值
     * @param rows      方格的行数
     * @param cols      方格的列数
     * @param row       当前处理的行号
     * @param col       当前处理的列号
     * @param visited   访问标记数组
     * @return 最多可走的方格
     */
    private static int movingCountCore(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
                    + 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;
    }

    /**
     * 断机器人可否进入坐标为(row, col)的方格
     *
     * @param threshold 约束值
     * @param rows      方格的行数
     * @param cols      方格的列数
     * @param row       当前处理的行号
     * @param col       当前处理的列号
     * @param visited   访问标记数组
     * @return 是否能够进入,true是,false否
     */
    private static boolean check(int threshold, int rows, int cols,
                                 int row, int col, boolean[] visited) {
        return col >= 0 && col < cols
                && row >= 0 && row < rows
                && !visited[row * cols + col]
                && (getDigitSum(col) + getDigitSum(row) <= threshold);
    }

    /**
     * 一个数字的数位之和
     *
     * @param number 数字
     * @return 数字的数位之和
     */
    private static int getDigitSum(int number) {
        int result = 0;
        while (number > 0) {
            result += (number % 10);
            number /= 10;
        }

        return result;
    }
}
相关文章
相关标签/搜索