c++

机器人的运动范围

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

class Solution {
public:
    int getDigitSum(int number) {
        int sum = 0;
        while(number) {
            sum+=number%10;
            number/=10;
        }
        return sum;
    }

    int movingCountCore(int threshold, int rows, int cols, int row, int col, vector<vector<bool> >& visit) {
        if (row < 0 || row >= rows || col < 0 || col >= cols) return 0;
        int count = 0;
        if (!visit[row][col] && getDigitSum(row)+getDigitSum(col) <= threshold) {
            visit[row][col] = true;
            ++ count;
            const int dx[4] = {-1, 0, 1, 0};
            const int dy[4] = {0, 1, 0, -1};
            for (int i = 0; i < 4; ++ i) {
                count +=  movingCountCore(threshold, rows, cols, row+dx[i], col+dy[i], visit);
            }
        }
        return count;
    }

    int movingCount(int threshold, int rows, int cols)
    {
        if (threshold <= 0 || rows <=0 || cols <= 0) return 0;
        vector<vector<bool> > visit(rows, vector<bool>(cols, false));
        return movingCountCore(threshold, rows, cols, 0, 0, visit);
    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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