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

机器人的运动范围

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

全部评论

相关推荐

08-14 13:51
门头沟学院 Java
没有任何游戏经验....
投递米哈游等公司10个岗位
点赞 评论 收藏
分享
08-08 16:33
唐山学院 Java
职场水母:首先,简历太长,对于实习和应届找工作,hr一眼扫的是学历,技术看实习,你写的技术栈字太多了,尽量用一句话概括不用写那么详细,技术面的时候会问的,而且技术栈都会在实习或者项目里体现,你要做的是,把你的简历浓缩为一页,删除没用的东西,比如实践经历,自我评价,这些纯废话,没用,专业技能写的太离谱,你真的熟练掌握了吗,建议都写熟悉,找工作和写论文不一样,追求的是干练和实用,把实习经历和项目提前,把掌握的技术栈写到最后,然后去找实习,
点赞 评论 收藏
分享
07-15 00:33
江苏大学 Java
代码飞升:哈哈哈哈评论区三个打广告的
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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