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

机器人的运动范围

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

回溯做法,遇到一个问题,形参前面&,一开始并不知道作用,后来发现是将形参与实参地址绑定,形参变化,实参也跟着变。
代码:

class Solution {
private:
    vector<vector<int>> ret;
public:
    int check(int n){
        int sum = 0;
        while(n){
            sum += n%10;
            n = n/10;
        }
        return sum;
    }

    int sea(int threshold, int rows, int cols, int x, int y, vector<vector<int>> &m){
        if(threshold < (check(x) + check(y)) || x<0 || y<0 || x>rows-1 || y>cols-1 || ret[x][y] == 1){
            return 0;
        }
        ret[x][y] = 1;
        return 1 + sea(threshold, rows, cols, x-1, y, ret) + sea(threshold, rows, cols, x+1, y, ret) + sea(threshold, rows, cols, x, y-1, ret) + sea(threshold, rows, cols, x, y+1, ret);
    }

    int movingCount(int threshold, int rows, int cols) {
        ret = vector<vector<int>> (rows, vector<int>(cols,0));
        return sea(threshold, rows, cols, 0, 0, ret);
    }
};
全部评论

相关推荐

昨天 15:12
门头沟学院 运营
面向对象的火龙果很爱...:去吃一顿炸鸡就走
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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