剑指 Offer 13. 机器人的运动范围

剑指 Offer 13. 机器人的运动范围

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

示例 1:

输入:m = 2, n = 3, k = 1
输出:3

示例 2:

输入:m = 3, n = 1, k = 0
输出:1

提示:

  • 1 <= n,m <= 100
  • 0 <= k <= 20

一、深度优先遍历DFS

根据K神思路写的代码:

class Solution {

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

    private int dfs(boolean[][] visited, int m, int n, int k, int i, int j) {
        if(i >= m || j >= n || visited[i][j] || bitSum(i) + bitSum(j) > k) return 0;
        visited[i][j] = true;
        return 1 + dfs(visited, m, n, k, i + 1, j) + dfs(visited, m, n, k, i, j + 1) ;
    }

    private int bitSum(int n) {
        int sum = 0;
        while(n > 0) {
            sum += n % 10;
            n /= 10; 
        }
        return sum;
    }
}

注释版本:

class Solution {
    // 棋盘的行列
    int m, n;
    // 记录位置是否被遍历过
    boolean[][] visited;

    public int movingCount(int m, int n, int k) {
        this.m = m;
        this.n = n;
        visited = new boolean[m][n];
        return dfs(0, 0, k);
    }

    private int dfs(int i, int j, int k) {
        // i >= m || j >= n是边界条件的判断
        if (i >= m || j >= n
                // visited[i][j]判断这个格子是否被访问过
                || visited[i][j] == true
                // k < sum(i, j)判断当前格子坐标是否满足条件
                || sum(i, j) > k) {
            return 0;
        }
        // 标注这个格子被访问过
        visited[i][j] = true;
        // 沿着当前格子的右边和下边继续访问
        return 1 + dfs(i + 1, j, k)
                + dfs(i, j + 1, k);
    }

    // 计算两个坐标数字的和
    private int sum(int i, int j) {
        int sum = 0;
        while (i != 0) {
            sum += i % 10;
            i /= 10;
        }
        while (j != 0) {
            sum += j % 10;
            j /= 10;
        }
        return sum;
    }
}

k神简洁的代码:

class Solution {
    int m, n, k;
    boolean[][] visited;
    public int movingCount(int m, int n, int k) {
        this.m = m; this.n = n; this.k = k;
        this.visited = new boolean[m][n];
        return dfs(0, 0, 0, 0);
    }
    public int dfs(int i, int j, int si, int sj) {
        if(i >= m || j >= n || k < si + sj || visited[i][j]) return 0;
        visited[i][j] = true;
        return 1 + dfs(i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj) + dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8);
    }
}

二、广度优先遍历 BFS

  • BFS/DFS : 两者目标都是遍历整个矩阵,不同点在于搜索顺序不同。DFS 是朝一个方向走到底,再回退,以此类推;BFS 则是按照“平推”的方式向前搜索。
  • BFS 实现: 通常利用队列实现广度优先遍历。

这个代码:有点繁冗,但可以让小白看懂代码是如何运行的。

class Solution {
    public int movingCount(int m, int n, int k) {
        //状态:dp[i][j]代表第i,j个格子能否走到
        boolean[][] dp = new boolean[m][n];
        dp[0][0] = isValid(0, 0, k);
        //转移方程
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(i == 0 && j == 0) continue;
                else if(i == 0) dp[i][j] = isValid(i, j, k) && dp[i][j - 1];
                else if(j == 0) dp[i][j] = isValid(i, j, k) && dp[i - 1][j];
                else dp[i][j] = isValid(i, j, k) && (dp[i - 1][j] || dp[i][j - 1]);
            }
        }
        int count = 0;
        for (boolean[] row : dp) {
            for (boolean ele : row) {
                if (ele) {
                    count++;
                }
            }
        }
        return count;
    }
     public boolean isValid(int i, int j, int k) {
        int sum = 0;
        while (i != 0) {
            sum += i % 10;
            i /= 10;
        }
        while (j != 0) {
            sum += j % 10;
            j /= 10;
        }
        return sum <= k;
    }
}

这个代码是为了求sum又简化了代码,但跟k神比起确实还差一点。

public int movingCount(int m, int n, int k) {
        boolean[][] nums = new boolean[m][n];
        nums[0][0] = isValid(0, 0, k);
        int sum = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0) {
                    continue;
                } else if (i == 0) {
                    nums[i][j] = nums[i][j - 1] && isValid(i, j, k);
                } else if (j==0) {
                    nums[i][j] = nums[i - 1][j] && isValid(i, j, k);
                } else {
                    nums[i][j] = (nums[i - 1][j] || nums[i][j - 1]) && isValid(i, j, k);
                }
                if (nums[i][j]) sum++;
            }
        }
        return sum;
    }
public boolean isValid(int m, int n, int k) {
        int sum = 0;
        while (m != 0 || n != 0) {
            if (m != 0) {
                sum += m % 10;
                m /= 10;
            }
            if (n != 0) {
                sum += n % 10;
                n /= 10;
            }
        }
        return sum <= k;
    }
}

k神更简洁的代码:

class Solution {
    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        int res = 0;
        Queue<int[]> queue= new LinkedList<int[]>();
        queue.add(new int[] { 0, 0, 0, 0 });
        while(queue.size() > 0) {
            int[] x = queue.poll();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res ++;
            queue.add(new int[] { i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            queue.add(new int[] { i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
}

参考链接:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/solution/mian-shi-ti-13-ji-qi-ren-de-yun-dong-fan-wei-dfs-b/

posted @ 2021-08-27 16:48  RainsX  阅读(35)  评论(0编辑  收藏  举报