题解 | #机器人的运动范围#

机器人的运动范围

https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

import java.util.*;
public class Solution {
    int step = 0;
    public int movingCount(int threshold, int rows, int cols) {
        boolean[][] tag = new boolean[100][100];
        checkMoving(threshold, rows, cols, 0, 0,tag);
        return step;
    }

    public void checkMoving(int threshold, int rows, int cols, int rowIndex,
                            int colIndex,boolean[][] tag) {
        if (rowIndex < 0 || colIndex < 0 || rowIndex >= rows || colIndex >= cols ||
                tag[rowIndex][colIndex]
           ) {
            return ;
        }
        tag[rowIndex][colIndex] = true;
        if ((sumResult(rowIndex) + sumResult(colIndex) > threshold)) {
            return;
        }
        step++;
        checkMoving(threshold, rows, cols, rowIndex + 1, colIndex,tag);
        checkMoving(threshold, rows, cols, rowIndex - 1, colIndex,tag);
        checkMoving(threshold, rows, cols, rowIndex, colIndex + 1,tag);
        checkMoving(threshold, rows, cols, rowIndex, colIndex - 1,tag);
    }

    private int sumResult(int num) {
        int sum = 0;
        do {
            sum += (num % 10);
            num = num / 10;
        } while (num > 0);
        return sum;
    }
}

#动态规划#
全部评论

相关推荐

03-14 18:48
重庆大学 C++
点赞 评论 收藏
分享
EEbond:给北邮✌️跪了
点赞 评论 收藏
分享
01-18 09:26
已编辑
门头沟学院 Java
王桑的大offer:建议中间件那块写熟悉即可,写掌握 面试包被拷打到昏厥
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务