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

机器人的运动范围

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

全部评论

相关推荐

05-19 15:21
已编辑
门头沟学院 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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