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

机器人的运动范围

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

#include <vector>
class Solution {
public:
    bool isvalid(int threshold, int rows, int cols, int x, int y){
        int sum = 0;
        if (x >= rows || y >= cols)
            return false;
        while (x != 0){
            sum += x % 10;
            x /= 10;
        }
        while (y != 0){
            sum += y % 10;
            y /= 10;
        }
        return sum > threshold ? false : true;
    }
    int result = 0;
    void backtracking(int threshold, int rows, int cols, int x, int y, vector<vector<bool>>& used){
        if (isvalid(threshold, rows, cols, x, y) && used[x][y] == true){
            used[x][y] = false;
            result++;
            backtracking(threshold, rows, cols, x + 1, y, used);
            backtracking(threshold, rows, cols, x, y + 1, used);
        }
        else 
            return;
    }

    int movingCount(int threshold, int rows, int cols) {
        vector<vector<bool>> used(rows, vector<bool>(cols, true));
        backtracking(threshold, rows, cols, 0, 0, used);
        return result;
    }
};

全部评论

相关推荐

06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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