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

机器人的运动范围

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

#include <vector>
class Solution 
{
public:
    int sum(int n)
    {
        int sum = 0;
        while(n)
        {
            sum += n%10;
            n /= 10;
        }
        return sum;
    }

    bool maxCount(int& threshold, int& rows, int& cols,vector<vector<bool>> &dp,int i,int j,int& max)
    {
        if(i < 0 || i >= rows || j < 0 || j >= cols || dp[i][j] || threshold < sum(i) + sum(j))
            return false;
        max++;
        dp[i][j] = true;

        int w[2][4] = {{0,1,0,-1},{1,0,-1,0}};

        int x,y;
        for(int k = 0;k < 4;k++)
        {
            x = i + w[0][k],y = j + w[1][k];
            if(maxCount(threshold,rows,cols,dp,x,y,max))
                return true;
        }

        return false;
    }

    int movingCount(int threshold, int rows, int cols) 
    {
        vector<vector<bool>> dp(rows,vector<bool>(cols,false));//记录格子是否走过
        int max = 0;

       maxCount(threshold,rows,cols,dp,0,0,max);
        return max;
    }
};

全部评论

相关推荐

菜鸡29号:根据已有信息能初步得出以下几点: 1、硕士排了大本和大专 2、要求会多语言要么是招人很挑剔要么就是干的活杂 3、给出校招薪资范围过于巨大,说明里面的薪资制度(包括涨薪)可能有大坑
点赞 评论 收藏
分享
云边有个小卖铺儿:校招生违约率低,所以我要高😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务