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

机器人的运动范围

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-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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