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

机器人的运动范围

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

#include <vector>
class Solution {
public:
    void dfs(int threshold, int rows, int cols, int i, int j, vector<vector<bool> > &hash) {
        if (i < 0 || i >= rows || j < 0 || j >= cols 
        || hash[i][j] || i/10 + i%10 + j/10 + j%10 > threshold) return;
        hash[i][j] = true;
        dfs(threshold, rows, cols, i-1, j, hash);
        dfs(threshold, rows, cols, i+1, j, hash);
        dfs(threshold, rows, cols, i, j-1, hash);
        dfs(threshold, rows, cols, i, j+1, hash);
    }
    int movingCount(int threshold, int rows, int cols) {
        vector<vector<bool> > hash(rows, vector<bool> (cols, false));
        dfs(threshold, rows, cols, 0, 0, hash);
        int res = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                res += hash[i][j] ? 1 : 0;
            }
        }
        return res;
    }
};

全部评论

相关推荐

03-27 15:39
算法工程师
几乎所有大中厂都挂了,现在压力拉满了。。。
ohs的小木屋:我摆了,该投投该面面,就当给自己找事做了
点赞 评论 收藏
分享
穿件外套出门:这简历一眼太水了,前面有的没的直接删,写项目亮点
点赞 评论 收藏
分享
03-20 18:39
已编辑
电子科技大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务