题解 | 机器人的运动范围

机器人的运动范围

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

#include <vector>
class Solution {
public:
    int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    int res = 0;

    int cal(int n)
    {
        int sum = 0;
        while(n)
        {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    void dfs(int i, int j, int rows, int cols, int threshold, vector<vector<bool>>& vis)
    {
        if(vis[i][j] || i < 0 || j < 0 || i >= rows || j >= cols) return;
        if(cal(i) + cal(j) > threshold) return;
        res ++;
        vis[i][j] = true;
        for(int k = 0; k < 4; k ++)
        {
            dfs(i + dir[k][0], j + dir[k][1], rows, cols, threshold, vis);
        }
    }

    int movingCount(int threshold, int rows, int cols) {
        if(threshold <= 0) return 1;
        vector<vector<bool> > vis(rows, vector<bool>(cols, false));
        dfs(0, 0, rows, cols, threshold, vis);
        return res;
    }

};

全部评论

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-30 18:19
个个985的硕士闭着眼睛都有15k以上的月薪,天天嚷嚷着研究生白度读了,天天嚷嚷着反向读研了........
MMMJC:不读研22本科出去的基本都拿28k呢,你不能用25的研究生和25的本科生比然后说没反向读研,而是25研和22本比呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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