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

机器人的运动范围

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;
    }
};

全部评论

相关推荐

12-24 20:52
武汉大学 Java
点赞 评论 收藏
分享
12-22 16:31
已编辑
桂林电子科技大学 Python
很奥的前端仔:如果你接了offer 临时又说不去 hr确实要多做一些工作。 当然如果是接offer之前当我没说
点赞 评论 收藏
分享
12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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