【剑指offer】11. 机器人的运动范围(java)

文章目录

机器人的运动范围

描述

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

数据范围: 0 ≤ t h r e s h o l d ≤ 15 0≤threshold≤15 0≤threshold≤15 ,$1≤rows,cols≤100 $

进阶:空间复杂度 O ( n m ) O(nm) O(nm) ,时间复杂度 $O(nm) $

示例1

输入:

1,2,3

返回值:

3

示例2

输入:

0,1,3

返回值:

1

示例3

输入:

10,1,100

返回值:

29

说明:

[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28] 这29种,后面的[0,29],[0,30]以及[0,31]等等是无法到达的

示例4

输入:

5,10,10

返回值:

21

思路

最容易想到的方法肯定就是遍历,对二维数组的每一位进行遍历,但需要考虑当数组下标不是一位数时需要取出每一位进行相加

java 复制代码
public int movingCount(int threshold, int rows, int cols) {
    int count = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int sum = 0;
            if (i > 10) {
                while (i > 0) {
                    sum += i % 10;
                    i /= 10;
                }
            } else {
                sum += i;
            }
            if (j > 10) {
                while (j > 0) {
                    sum += j % 10;
                    j /= 10;
                }
            } else {
                sum += j;
            }
            if (sum <= threshold) {
                count++;
            }
        }
    }
    return count;
}

但这种情况下对于较大规模的数据容易超时

下面使用递归来解决这个问题

  • 首先需要定义一个数组access用于记录当前元素是否已经访问过
  • 从左上角元素开始递归
  • 每次递归先判断是否达到边界条件
    • 超过矩阵边界直接返回
    • 横纵坐标数位之和大于threshold直接返回
    • 当前节点已访问直接返回
  • 如果未访问过当前节点,则结果加一,并表姐当前节点为已访问
  • 接着递归当前节点的四个邻居节点,直到递归结束

完整代码

java 复制代码
import java.util.*;
public class Solution {
    boolean[][] access;
    int result = 0;
    public int movingCount(int threshold, int rows, int cols) {
        access = new boolean[rows][cols];
        BackTracking(threshold, 0, 0);
        return result;
    }

    public void BackTracking(int threshold, int row, int col) {
        if (row < 0 || col < 0 || row >= access.length || col >= access[0].length) {
            return;
        }
        if (cal(row, col) > threshold) {
            return;
        }

        if (access[row][col] == false) {
            result++;
            access[row][col] = true;
        } else {
            return;
        }      
        BackTracking(threshold, row + 1, col);
        BackTracking(threshold, row, col - 1);
        BackTracking(threshold, row, col + 1);
        BackTracking(threshold, row - 1, col);
        return;
    }
    public int cal(int row, int col) {
        int sum = 0;
        while (row > 0) {
            sum += row % 10;
            row = row / 10;
        }
        while (col > 0) {
            sum += col % 10;
            col = col / 10;
        }
        return sum;
    }
}
相关推荐
南山十一少2 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
Hylan_J3 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶3 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
427724003 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo3 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦3 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个3 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
失败尽常态5236 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447746 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农6 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx