java 剑指offer之[数据结构 困难JZ13 机器人的运动范围

题目的链接在这里:https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8


题目大意

地上有一个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。请问该机器人能够达到多少个格子?

一、示意图

在这里插入图片描述

二、解题思路

DFS和BFS

DFS

代码如下:

public class Solution {
    int x[]={0,0,1,-1};
        int y[]={1,-1,0,0};
        int count=0;
        boolean isVisited[][]=new boolean[100][100];
        public int movingCount(int threshold, int rows, int cols) {
                //BFS还是DFS应该都可以吧 然后还有个方法是位数之和
                //从起始点开始
                int i=0;
                int j=0;

                Dfs(i,j,threshold,rows,cols);

                return count;
        }

        private void Dfs(int i, int j, int threshold, int rows, int cols) {
                if(i<0||i>=rows||j<0||j>=cols||isVisited[i][j]||!isgetNum(i,j,threshold)){
                        //这些都不满足
                        return;
                }
                //不然的话 就可以增加
                count++;
                isVisited[i][j]=true;
                //然后四个角度遍历
                for(int step=0;step<4;step++){
                        Dfs(i+x[step],j+y[step],threshold,rows,cols);
                }
    /*            //直接在这边判断
                if((i==rows-1)&&(j==cols-1))
                        //说明达到终点了
                        return;

                for(int step=0;step<4;step++){
                       int newi=i+x[step];
                       int newj=j+y[step];
                       //看看满不满足条件 没有越界和数值 然后是没有被访问过
                        if(newi<rows&&newi>=0&&newj<cols&&newj>=0&&isgetNum(newi,newj,threshold)&&!isVisited[newi][newj]){
                                //说明这个位置可以
                                count++;
                                //把它设置成访问过了
                                isVisited[newi][newj]=true;
                                //然后继续递归新的
                                Bfs(newi,newj,threshold,rows,cols);
                        }
                }*/

                }

        private boolean isgetNum(int i, int j, int threshold) {
                     int num=0;
                while (i!=0){
                        num+=(i%10);
                        i/=10;
                }
                while (j!=0){
                        num+=(j%10);
                        j/=10;
                }
                return num<=threshold;
        }
}

在这里插入图片描述

BFS

代码如下:

import java.util.LinkedList;
import java.util.Queue;
public class Solution {
    int stepX[]={0,0,1,-1};
        int stepY[]={1,-1,0,0};
        int count=0;
        boolean isVisited[][]=new boolean[100][100];
        public int movingCount(int threshold, int rows, int cols) {
                //BFS还是DFS应该都可以吧 然后还有个方法是位数之和
                //从起始点开始
                //然后使用BFS 使用队列
                Queue<Integer> queue=new LinkedList<>();
                //一次放入两个吧
                //00先进去
                queue.add(0);
                queue.add(0);
                isVisited[0][0]=true;
                while (!queue.isEmpty()){
                        //然后一口气放出两个 当然能出来就说明可以加一
                        count++;
                        int x=queue.poll();
                        int y=queue.poll();
                        
                        //这个就是对应的坐标 然后判断条件 进栈的肯定是满足条件的
                        for(int i=0;i<4;i++){
                                int newx=x+stepX[i];
                                int newy=y+stepY[i];
                                //然后进行判断 设置他是满足条件的
                                if(newx>=0&&newx<rows&&newy>=0&&newy<cols&&!isVisited[newx][newy]&&isgetNum(newx,newy,threshold)){
                                        //这些条件都满足的话 就进栈 并设置为访问过了
                                        isVisited[newx][newy]=true;
                                        queue.add(newx);
                                        queue.add(newy);
                                }
                        }
                }
                return count;
        }



        private boolean isgetNum(int i, int j, int threshold) {
                int num=0;
                while (i!=0){
                        num+=(i%10);
                        i/=10;
                }
                while (j!=0){
                        num+=(j%10);
                        j/=10;
                }
                return num<=threshold;
        }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值