题解 | #机器人的运动范围 岛屿数量问题类似#

机器人的运动范围

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

public class Solution {
    public int movingCount(int threshold, int rows, int cols) {
        boolean[][] dp = new boolean[rows][cols];
        process(dp, 0, 0 , rows, cols, threshold);
        int ans = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (dp[i][j] == true){
                    ans++;
                }
            }
        }
        return ans;
    }
    public static void process(boolean[][] dp, int x, int y, int rows ,int cols, int threshold){
        if (x < 0 || x >= rows || y < 0 || y >= cols
           || !withinThreshold(x, y, threshold)
                || dp[x][y] == true){
            return;
        }
        dp[x][y] = true;
        process(dp, x + 1, y, rows, cols, threshold);
        process(dp, x - 1, y, rows, cols, threshold);
        process(dp, x, y - 1, rows, cols, threshold);
        process(dp, x, y + 1, rows, cols, threshold);
    }
    public static boolean withinThreshold(int x, int y, int threshold){
        int sum = x / 10 + y / 10 + x % 10 + y % 10;
        return sum <= threshold;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-05 15:27
点赞 评论 收藏
分享
真烦好烦真烦:牛友太有实力了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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