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

机器人的运动范围

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

public class Solution {
    public int movingCount(int threshold, int rows, int cols) {
        if (threshold == 0) {
            return 1;
        }
        int[][] sign = new int[rows][cols];
        return process(0, 0, rows, cols, threshold, sign);
    }
    public static int process(int x, int y, int m, int n, int k, int[][] sign) {
        if (x < 0 || x >= m || y < 0 || y >= n || sign[x][y] == 1 || !isValid(x, y, k)) {
            return 0;
        }
        sign[x][y] = 1;
        return 1 + process(x - 1, y, m, n, k, sign) + process(x + 1, y, m, n, k, sign) + process(x, y - 1, m, n, k, sign) + process(x, y + 1, m, n, k, sign);
    }
    public static boolean isValid(int x, int y, int k) {
        int sum = 0;
        while (x != 0 && y != 0) {
            sum += x % 10 + y % 10;
            x /= 10;
            y /= 10;
        }
        while (x != 0) {
            sum += x % 10;
            x /= 10;
        }
        while (y != 0) {
            sum += y % 10;
            y /= 10;
        }
        return sum <= k ? true : false;
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 16:08

相关推荐

今天投了小鹏,收到了AI面,大概会问哪些啊?
期末一定及格:总共4个部分,心理测评、行测、然后就是问岗位、对岗位的理解、过往遇到了哪些难点怎么解决,很简单,没有什么特别专业的问题,都是一些综合素质相关的
小鹏汽车AI面7人在聊
点赞 评论 收藏
分享
北漂的牛马人:211佬,包进的,可能是系统问题
点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务