LeetCode(剑指 Offer)- 13. 机器人的运动范围

简介: LeetCode(剑指 Offer)- 13. 机器人的运动范围

题目链接:点击打开链接

题目大意:

解题思路


9.png

相关企业

  • Facebook

AC 代码

  • Java


// 解决方案(1)
class Solution {
    private int row, col, count, limit;
    private boolean[][] path, no;
    private int[] calc;
    public int movingCount(int m, int n, int k) {
        limit = k;
        count = 0;
        row = m;
        col = n;
        path = new boolean[m][n];
        no = new boolean[m][n];
        calc = new int[m * n];
        dfs(0, 0);
        return count;
    }
    private void dfs(int i, int j) {
        if (i < 0 || i >= row || j < 0 || j >= col || path[i][j]) {
            return;
        }
        path[i][j] = true;
        if (cal(i) + cal(j) <= limit) {
            count++;
        } else {
            no[i][j] = true;
            // 此时站在限制位置, 必须马上回退, 这个最容易忘记
            return;
        }
        // 接下来 4 个 if 为的是不进限制位置
        if (i+1 < row && !no[i+1][j]) {
            dfs(i + 1, j);
        }
        if (i-1 >= 0 && !no[i-1][j]) {
            dfs(i - 1, j);
        }
        if (j+1 < col && !no[i][j+1]) {
            dfs(i, j + 1);
        }
        if (j-1 >= 0 && !no[i][j-1]) {
            dfs(i, j - 1);
        }
    }
    private int cal(int num) {
        int tnum = num;
        if (calc[tnum] != 0) {
            return calc[tnum];
        }
        int sum = 0;
        while (num != 0) {
            sum += num % 10;
            num /= 10;
        }
        calc[tnum] = sum;
        return sum;
    }
}
// 解决方案(2)
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);
    }
}
// 解决方案(3)
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;
    }
}
  • C++


// 解决方案(1)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector<vector<bool>> visited(m, vector<bool>(n, 0));
        return dfs(0, 0, 0, 0, visited, m, n, k);
    }
private:
    int dfs(int i, int j, int si, int sj, vector<vector<bool>> &visited, int m, int n, int k) {
        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, visited, m, n, k) +
                   dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8, visited, m, n, k);
    }
};
// 解决方案(2)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector<vector<bool>> visited(m, vector<bool>(n, 0));
        int res = 0;
        queue<vector<int>> que;
        que.push({ 0, 0, 0, 0 });
        while(que.size() > 0) {
            vector<int> x = que.front();
            que.pop();
            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++;
            que.push({ i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            que.push({ i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
};  
目录
相关文章
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
数据可视化 机器人 Python
实例9:四足机器人运动学正解平面RR单腿可视化
本文是关于四足机器人正向运动学(FK)的实例教程,通过Python编程实现了简化的mini pupper平面二连杆模型的腿部可视化,并根据用户输入的关节角计算出每个关节相对于基坐标系的坐标。
46 1
|
2月前
|
机器人
MATLAB - 机器人任务空间运动模型
MATLAB - 机器人任务空间运动模型
33 1
|
2月前
|
机器人
PUN ☀️六、机器人基础设置:运动、相机、攻击与生命值
PUN ☀️六、机器人基础设置:运动、相机、攻击与生命值
|
2月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
17 3
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
30 3
|
2月前
|
数据可视化 算法 机器人
实例10:四足机器人运动学逆解可视化与实践
本文是关于四足机器人逆运动学(IK)的实例教程,介绍了逆运动学的概念、求解方法、多解情况和工作空间,并通过Python编程实现了简化的mini pupper平面二连杆模型的逆运动学可视化,包括单腿舵机的校准和动态可视化运动学计算结果。
53 0
|
2月前
|
机器人 vr&ar
MATLAB - 移动机器人运动学方程
MATLAB - 移动机器人运动学方程
36 0
|
2月前
|
机器人 Serverless
MATLAB - 机器人关节空间运动模型
MATLAB - 机器人关节空间运动模型
24 0
|
2月前
|
存储 算法 数据可视化
MATLAB - 机器人逆运动学设计器(Inverse Kinematics Designer APP)
MATLAB - 机器人逆运动学设计器(Inverse Kinematics Designer APP)
42 0
下一篇
无影云桌面