• LeetCode树系列(3)——200题岛屿数量


      这种在图中找岛屿的题目我们在前面已经讲过了,这个题目同样属于这类题目,我们很自然想到的就是DFS和BFS方法。

    一、题目描述

      给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量

      岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

      此外,你可以假设该网格的四条边均被水包围。

    示例 1:
    输入:
    11110
    11010
    11000
    00000
    输出: 1
    示例 2:
    输入:
    11000
    11000
    00100
    00011
    输出: 3
    解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。

    二、算法思想

    1、DFS

    对于DFS就要使用栈:

    • 遍历,将第一个路地块的坐标入栈。
    • 将给路地块周围的路地块沉没,并将其坐标入栈。
    • 以此将队列中的元素出栈,直到栈为空,岛屿数加1。

    2、BFS

    对于BFS就要使用队列:

    • 遍历,将第一个路地块的坐标入队列。
    • 将给路地块周围的路地块沉没,并将其坐标入队列。
    • 以此将队列中的元素出队列,指导队列为空,岛屿数加1。

    三、代码实现

    import java.util.ArrayDeque;
    import java.util.Queue;
    import java.util.Stack;
    
    public class leetCode200 {
        public static void main(String[] args){
            char [][]grid={{'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'}};
            System.out.println(numIslands(grid));
        }
        
        // 广度搜索
        private static int numIslands(char[][] grid) {
            if(grid==null||grid.length==0) return 0;
            int ans=0;
            int[] dx = {0, 0, 1, -1};
            int[] dy = {1, -1, 0, 0};
            Queue<int[]> queue = new ArrayDeque<>();
            int m = grid.length, n = grid[0].length;
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    if (grid[i][j] == '1') {
                        queue.offer(new int[] {i, j});
                        grid[i][j] = '0';
                        ans++;
                        int[] point=null;
                        while(!queue.isEmpty()){
                            point=queue.poll();
                            int x=point[0],y=point[1];
                            for(int k=0;k<4;++k){
                                int newx=x+dx[k];
                                int newy=y+dy[k];
                                if (newx < 0 || newx >= m || newy < 0 || newy >= n )
                                    continue;
                                if (grid[newx][newy] == '1') {
                                    queue.offer(new int[] {newx, newy});
                                    grid[newx][newy]='0';
                                }
                            }
                        }
                    }
            return ans;
        }
    
        // 深度搜索
        private static int numIslands1(char[][] grid) {
            if(grid==null||grid.length==0) return 0;
            int ans=0;
            int[] dx = {0, 0, 1, -1};
            int[] dy = {1, -1, 0, 0};
            Stack<int[]> stack = new Stack<>();
            int m = grid.length, n = grid[0].length;
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    if (grid[i][j] == '1') {
                        stack.push(new int[] {i, j});
                        grid[i][j] = '0';
                        ans++;
                        int[] point=null;
                        while(!stack.isEmpty()){
                            point=stack.pop();
                            int x=point[0],y=point[1];
                            for(int k=0;k<4;++k){
                                int newx=x+dx[k];
                                int newy=y+dy[k];
                                if (newx < 0 || newx >= m || newy < 0 || newy >= n )
                                    continue;
                                if (grid[newx][newy] == '1') {
                                    stack.push(new int[] {newx, newy});
                                    grid[newx][newy]='0';
                                }
                            }
                        }
                    }
            return ans;
        }
    }
  • 相关阅读:
    Android OpenGL ES 2.0 (四) 灯光perfragment lighting
    Android OpenGL ES 2.0 (五) 添加材质
    冒泡排序函数
    javascript object 转换为 json格式 toJSONString
    Liunx CentOS 下载地址
    jquery 图片切换特效 鼠标点击左右按钮焦点图切换滚动
    javascript 解析csv 的function
    mysql Innodb Shutdown completed; log sequence number解决办法
    Centos 添加 yum
    javascript 键值转换
  • 原文地址:https://www.cnblogs.com/SupremeBoy/p/12736725.html
Copyright © 2020-2023  润新知