dfs 机器人的运动范围,计算派出机器人的数量,岛屿数量

机器人的运动范围

在这里插入图片描述

import java.util.*;
public class Solution {
    public int movingCount(int threshold, int rows, int cols) {
         boolean[][] visited = new boolean[rows][cols];
        return test(0,0,rows,cols,threshold,visited);
    }
    public int test(int x,int y,int rows,int cols,int k,boolean[][]visited){
        if(x<0||x>=rows||y<0||y>=cols||x%10+x/10+y%10+y/10>k||visited[x][y]){
            return 0;
        }
        //把现在访问得到标记一下
        visited[x][y]=true;
        return 1+test(x+1,y,rows,cols,k,visited)+
            test(x,y+1,rows,cols,k,visited)+
            test(x,y+1,rows,cols,k,visited)+
            test(x,y-1,rows,cols,k,visited);
        
    }
}

计算派出机器人的数量

在这里插入图片描述
在这里插入图片描述

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * @param grid char字符型二维数组
     * @return int整型
     */
    public int numIslands (char[][] grid) {
        int m=grid.length;
        // write code here
        int count=0;
        int n=grid[0].length;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]=='1'){
                   
                    dfs(grid,i,j);
                      count++;
                }
            }
        }
          return count;
    }
     public void dfs(char[][]grid,int x,int y){
         if(x<0||x>=grid.length||y<0||y>=grid[0].length||grid[x][y]=='0'){
             return;
         }
         grid[x][y]='0';
         dfs(grid,x+1,y);
         dfs(grid,x-1,y);
         dfs(grid,x,y+1);
         dfs(grid,x,y-1);
      
     }
}

岛屿数量

在这里插入图片描述

import java.util.*;


public class Solution {
    /**
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */
    public int solve (char[][] grid) {
        // write code here
        int m=grid.length;
        int n=grid[0].length;
        int count=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]=='1'){
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
       return count;
    }
    public void dfs(char[][]grid,int x,int y){
        if(x<0||x>=grid.length||y<0||y>=grid[0].length||grid[x][y]=='0'){
            return;
        }
        grid[x][y]='0';
        dfs(grid,x+1,y);
        dfs(grid,x-1,y);
        dfs(grid,x,y+1);
        dfs(grid,x,y-1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用深度优先搜索(DFS)实现机器人寻路程序的示例代码: ```python # 定义迷宫地图和机器人起点 maze = [ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0] ] start = (1, 1) # 定义 DFS def dfs(maze, start, visited): if start in visited: return False if maze[start[0]][start[1]] == 1: return False if maze[start[0]][start[1]] == 2: print("找到终点:", start) return True visited.append(start) directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] for d in directions: next_pos = (start[0] + d[0], start[1] + d[1]) if dfs(maze, next_pos, visited): return True return False # 调用 DFS dfs(maze, start, []) ``` 在上述代码中,我们首先定义了一个迷宫地图和机器人的起点。然后,我们定义了一个 `dfs` 函来实现 DFS 算法。该函接收三个参:迷宫地图、机器人起点和已访问过的位置列表。在每个位置,我们检查该位置是否已被访问过、是否为墙壁或终点。如果是终点,我们就输出该位置并返回 True。否则,我们将该位置添加到已访问过的位置列表中,并继续遍历该位置的上下左右四个方向。如果没有找到终点,我们就返回 False。 最后,我们调用 `dfs` 函,并将已访问过的位置列表初始化为空列表 `[]`。如果机器人能够找到终点, `dfs` 函将返回 True,否则返回 False。在本例中,机器人能够从起点 (1, 1) 找到终点 (3, 3)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值