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

机器人的运动范围

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

相关推荐

屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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