【剑指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;
    }
}
相关推荐
农民小飞侠5 分钟前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
战神刘玉栋7 分钟前
《程序猿之设计模式实战 · 观察者模式》
python·观察者模式·设计模式
敲代码不忘补水9 分钟前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践
陈大爷(有低保)1 小时前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu1 小时前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
王哲晓1 小时前
Linux通过yum安装Docker
java·linux·docker
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存2 小时前
源码分析:LinkedList
java·开发语言
子午2 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn