剑指offer 机器人的运动范围

题目描述

在这里插入图片描述

分析

思路:
题目中给出的条件告诉我们机器人可以到某个点的条件是横纵坐标的各个位数之和小于k。相当于告诉了我们某个点的判断方法。
题目要求是求得机器人能够到达多少个格子,也就是说需要我们推得它可以去哪些点,我能想到的就是根据它当前可以到达的点向其四周寻找可以前行的点,然后再以新的点去向四周开始新的搜索,最终所能到达的点的总和就是所求格子数。
如何去重?
初始化一个二维数组,类型为布尔值,每当机器人经过一个点(判断完一个点)就把这个位置对应的值置为true,在进入某个点之前也会加上判断,这样就可以防止反复了。
几个方法:
find方法:它是递归的主体,模仿了机器人的运动。

    public int find(int rows, int cols){
        int count  = 0;
        if(rows >= 0 && rows <rowMax && cols >= 0 && cols < colMax && check(thre,rows,cols) == true && s[rows][cols] == false) {
            s[rows][cols] = true;
            return 1 + find(rows + 1, cols) + find(rows - 1, cols) + find(rows, cols + 1) + find(rows, cols - 1);
        }
        return count;
    }

check方法:判断某点是否满足题目中的那个条件。

public boolean check(int threshold, int rows, int cols){
                int sum1 = 0;
                int sum2 = 0;
                int num1 = rows;
                int num2 = cols;
                while(num1 > 0) {
                    sum1 += num1 % 10;
                    num1 = num1 / 10;
                }
                while(num2 > 0) {
                    sum2 += num2 % 10;
                    num2 = num2 / 10;
                }
                if(sum1 + sum2 <= threshold) {
                    return true;
                }else {
                    return false;
                }
    }

get方法:获取各个位数之和。

    public int get(int num){
        int sum = 0;
        
        while(num > 0) {
            sum += num % 10;
            num = num / 10;
        }
        return sum;
    }

完整代码

public class Solution {
    int rowMax;
    int colMax;
    int thre;
    boolean[][] s;
    public int movingCount(int threshold, int rows, int cols)
    {
        rowMax = rows;
        colMax = cols;
        thre = threshold;
        s = new boolean[rows][cols];
        int count = find(0, 0);
        return count;
    }
    
    public int find(int rows, int cols){
        int count  = 0;
        if(rows >= 0 && rows <rowMax && cols >= 0 && cols < colMax && check(thre,rows,cols) == true && s[rows][cols] == false) {
            s[rows][cols] = true;
            return 1 + find(rows + 1, cols) + find(rows - 1, cols) + find(rows, cols + 1) + find(rows, cols - 1);
        }
        return count;
    }
    
    public boolean check(int threshold, int rows, int cols){
                int sum1 = 0;
                int sum2 = 0;
                int num1 = rows;
                int num2 = cols;
                while(num1 > 0) {
                    sum1 += num1 % 10;
                    num1 = num1 / 10;
                }
                while(num2 > 0) {
                    sum2 += num2 % 10;
                    num2 = num2 / 10;
                }
                if(sum1 + sum2 <= threshold) {
                    return true;
                }else {
                    return false;
                }
    }
    
    public int get(int num){
        int sum = 0;
        
        while(num > 0) {
            sum += num % 10;
            num = num / 10;
        }
        return sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值