BFS、DFS——机器人的运动范围

题目描述

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

个人感觉本题更适合使用BFS。

若使用BFS,则思考过程如下:

1.矩阵中找从(0,0)作为起始顶点的最大顶点可达数。

2.队列加入起始顶点(0,0),flag[0][0]置位true,count置为1。

3.对满足条件的邻接点加入队列,置位flag,更新count。

4.队列的结点应为坐标(x,y)的Pair。

import java.util.*;
public class Solution {
    public int movingCount(int k, int rows, int cols)
    {
        if(rows < 1||cols < 1||k < 0)
            return 0;
        boolean[][] flag=new boolean[rows][cols];
        LinkedList<Pair> list=new LinkedList();
        list.add(new Pair(0,0));
        flag[0][0]=true;
        int count=1;
        while(list.size()!=0){
            Pair pair=list.pollFirst();
            int x=pair.x;
            int y=pair.y;
            if(can_reach(rows,cols,x-1,y,k,flag)){
                list.add(new Pair(x-1,y));
                count++;
                flag[x-1][y]=true;
            }
            if(can_reach(rows,cols,x+1,y,k,flag)){
                list.add(new Pair(x+1,y));
                count++;
                flag[x+1][y]=true;
            }
            if(can_reach(rows,cols,x,y-1,k,flag)){
                list.add(new Pair(x,y-1));
                count++;
                flag[x][y-1]=true;
            }
            if(can_reach(rows,cols,x,y+1,k,flag)){
                list.add(new Pair(x,y+1));
                count++;
                flag[x][y+1]=true;
            }
        }
        return count;
    }
    //判断该坐标的格子是否满足要求
    public boolean can_reach(int rows,int cols,int x,int y,int k,boolean[][] flag){
        if(x < 0||x == rows||y < 0||y == cols||flag[x][y] == true)
            return false;
        int sum=0;
        while(x!=0){
            sum+=x%10;
            x/=10;
        }
        while(y!=0){
            sum+=y%10;
            y/=10;
        }
        return sum<=k?true:false;
    }
    class Pair{
        int x;
        int y;
        public Pair(int x,int y){
            this.x=x;
            this.y=y;
        }
    }
}

若分上下左右四次添加顶点代码复杂的话,可以这样子:

//保存邻接点选择方案
        int[][] path=new int[4][2];
        fill(path);
        while(list.size()!=0){
            Pair pair=list.pollFirst();
            int x=pair.x;
            int y=pair.y;
            for(int i=0;i<path.length;i++){
                int xx=x+path[i][0];
                int yy=y+path[i][1];
                if(can_reach(rows,cols,xx,yy,k,flag)){
                    list.add(new Pair(xx,yy));
                    flag[xx][yy]=true;
                    count++;
                }
            }
        }
        return count;
    }
    //用矩阵保存邻接点方案
    public void fill(int[][] path){
         path[0][0]=-1;//向上
        path[0][1]=0;
         path[1][0]=1;//向下
        path[1][1]=0;
         path[2][0]=0;//向左
        path[2][1]=-1;
         path[3][0]=0;//向右
        path[3][1]=1;
    }

若使用DFS,则思考过程如下:

1.矩阵中找从(0,0)作为起始顶点的最大顶点可达数。
2.起始顶点为(0,0)。
3.邻接点为上下左右。
4.flag标记走过的顶点。

public class Solution {
    public int movingCount(int k, int rows, int cols)
    {
        if(rows < 1||cols < 1||k < 0)
            return 0;
        boolean[][] flag=new boolean[rows][cols];
        return dfs(rows,cols,0,0,k,flag);
    }
    public int dfs(int rows,int cols,int i,int j,int k,boolean[][] flag){
        if(i < 0||i == rows||j < 0||j == cols||can_reach(i,j,k) == false||flag[i][j] == true)
            return 0;
        flag[i][j]=true;
        int result=dfs(rows,cols,i-1,j,k,flag)+dfs(rows,cols,i+1,j,k,flag)+
                   dfs(rows,cols,i,j-1,k,flag)+dfs(rows,cols,i,j+1,k,flag)+1;
        return result;
    }
    public boolean can_reach(int x,int y,int k){
        int sum=0;
        while(x!=0){
            sum+=x%10;
            x/=10;
        }
        while(y!=0){
            sum+=y%10;
            y/=10;
        }
        return sum<=k?true:false;
    }




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值