機器人的運動範圍

地上有一個 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;
    }
}
相關文章
相關標籤/搜索