《劍指 Offer》面試題13. 機器人的運動範圍(DFS、BFS)

地址:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/

注意:只需要兩個方向。

方法一:深度優先遍歷

Java 代碼:

public class Solution {

    // 深度優先遍歷

    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        int[][] directions = {{0, 1}, {1, 0}};

        return dfs(0, 0, k, m, n, visited, directions);
    }


    private int dfs(int x, int y, int k, int m, int n, boolean[][] visited, int[][] directions) {
        visited[x][y] = true;
        int count = 1;

        for (int[] direction : directions) {
            int newX = x + direction[0];
            int newY = y + direction[1];

            if (inArea(newX, newY, m, n) && !visited[newX][newY] && getDigitSum(newX) + getDigitSum(newY) <= k) {
                count += dfs(newX, newY, k, m, n, visited, directions);
            }
        }
        return count;
    }

    /**
     * @param num 一個整數的數位之和
     * @return
     */
    private int getDigitSum(int num) {
        int sum = 0;
        while (num > 0) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }

    /**
     * @param x    二維表格單元格橫座標
     * @param y    二維表格單元格縱座標
     * @param rows 二維表格行數
     * @param cols 二維表格列數
     * @return
     */
    private boolean inArea(int x, int y, int rows, int cols) {
        return x >= 0 && x < rows && y >= 0 && y < cols;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int m = 2;
        int n = 3;
        int k = 1;

//        int m = 3;
//        int n = 1;
//        int k = 0;
        int res = solution.movingCount(m, n, k);
        System.out.println(res);
    }
}

方法二:廣度優先遍歷

Java 代碼:

import java.util.LinkedList;
import java.util.Queue;

public class Solution {

    // 廣度優先遍歷

    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        int[][] directions = {{0, 1}, {1, 0}};

        Queue<Integer> queue = new LinkedList<>();
        queue.add(getIndex(0, 0, n));

        int res = 0;
        while (!queue.isEmpty()) {
            Integer head = queue.poll();

            int currentX = head / n;
            int currentY = head % n;

            visited[currentX][currentY] = true;
            res++;

            for (int[] direction : directions) {
                int newX = currentX + direction[0];
                int newY = currentY + direction[1];

                if (inArea(newX, newY, m, n) && !visited[newX][newY] && getDigitSum(newX) + getDigitSum(newY) <= k) {
                    queue.add(getIndex(newX, newY, n));
                    // 注意:添加到隊列的時候,就要佔住
                    visited[newX][newY] = true;
                }
            }
        }
        return res;
    }

    /**
     * @param num 一個整數的數位之和
     * @return
     */
    private int getDigitSum(int num) {
        int sum = 0;
        while (num > 0) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }

    /**
     * @param x    二維表格單元格橫座標
     * @param y    二維表格單元格縱座標
     * @param rows 二維表格行數
     * @param cols 二維表格列數
     * @return
     */
    private boolean inArea(int x, int y, int rows, int cols) {
        return x >= 0 && x < rows && y >= 0 && y < cols;
    }

    /**
     * @param x    二維表格單元格橫座標
     * @param y    二維表格單元格縱座標
     * @param cols 二維表格列數
     * @return
     */
    private int getIndex(int x, int y, int cols) {
        return x * cols + y;
    }


    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int m = 2;
        int n = 3;
        int k = 1;

//        int m = 3;
//        int n = 1;
//        int k = 0;
        int res = solution2.movingCount(m, n, k);
        System.out.println(res);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章