前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >机器人的运动范围_66

机器人的运动范围_66

作者头像
名字是乱打的
发布2022-05-13 12:49:36
2160
发布2022-05-13 12:49:36
举报
文章被收录于专栏:软件工程

描述 地上有一个rows行和cols列的方格。坐标从 [0,0] 到 [rows-1,cols-1]。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于threshold的格子。 例如,当threshold为18时,机器人能够进入方格[35,37],因为3+5+3+7 = 18。但是,它不能进入方格[35,38],因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

范围: 1 <= rows, cols<= 100 0 <= threshold <= 20

代码语言:javascript
复制
public class Solution {
 public int movingCount(int threshold, int rows, int cols) {
        boolean [][] isState= new boolean [rows][cols]; //记录是否走过
        int sum=help( threshold,  rows,  cols,0,0,isState);
        return sum;
    }
    public int help(int k, int m, int n,int r,int c,boolean [][] isState){
        if (numSum(r)+numSum(c)>k){//如果该点数为和大于k
            return 0;
        }
        if (r<0||r>=m||c<0||c>=n){ //如果该点不在方格范围之内
            return 0;
        }
        if (!isState[r][c]){
            isState[r][c]=true;
               return help(k,m,n,r,c+1,isState)
                +help(k,m,n,r,c-1,isState)
                +help(k,m,n,r+1,c,isState)
                +help(k,m,n,r-1,c,isState)
                +1
                ;
        }else {
            return 0;
        }

    }

    //每位上的数字之和
    public int numSum(int num){
        int res=0;
        while (num!=0){
            res+= num % 10;
            num=num/10;
        }
        return res;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档