【leetcode-200 深度优先+广度优先】 岛屿数量

给定一个由 ‘1‘(陆地)和 ‘0‘(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:
11110
11010
11000
00000

输出: 1
示例 2:

输入:
11000
11000
00100
00011

输出: 3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands

方法一:深度优先搜索 【通过】
直觉

将二维网格看成一个无向图,竖直或水平相邻的 1 之间有边。

算法

线性扫描整个二维网格,如果一个结点包含 1,则以其为根结点启动深度优先搜索。在深度优先搜索过程中,每个访问过的结点被标记为 0。计数启动深度优先搜索的根结点的数量,即为岛屿的数量。

class Solution {
  void dfs(char[][] grid, int r, int c) {
    int nr = grid.length;
    int nc = grid[0].length;

    if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == ‘0‘) {
      return;
    }

    grid[r][c] = ‘0‘;
    dfs(grid, r - 1, c);
    dfs(grid, r + 1, c);
    dfs(grid, r, c - 1);
    dfs(grid, r, c + 1);
  }

  public int numIslands(char[][] grid) {
    if (grid == null || grid.length == 0) {
      return 0;
    }

    int nr = grid.length;
    int nc = grid[0].length;
    int num_islands = 0;
    for (int r = 0; r < nr; ++r) {
      for (int c = 0; c < nc; ++c) {
        if (grid[r][c] == ‘1‘) {
          ++num_islands;
          dfs(grid, r, c);
        }
      }
    }

    return num_islands;
  }
}

方法二:广度优先遍历

除了 “深度优先遍历”,你还可以使用 “广度优先遍历”,此时你就不用回溯了。“广度优先遍历” 需要一个 “辅助队列”。

深度优先使用递归,一个方向走到底,广度优先使用一个辅助队列,先处理一个点的所有直接相邻节点

class Solution {
  public int numIslands(char[][] grid) {
    if (grid == null || grid.length == 0) {
      return 0;
    }

    int nr = grid.length;
    int nc = grid[0].length;
    int num_islands = 0;

    for (int r = 0; r < nr; ++r) {
      for (int c = 0; c < nc; ++c) {
        if (grid[r][c] == ‘1‘) {
          ++num_islands;
          grid[r][c] = ‘0‘; // mark as visited
          Queue<Integer> nei***ors = new LinkedList<>();
          nei***ors.add(r * nc + c);
          while (!nei***ors.isEmpty()) {
            int id = nei***ors.remove();
            int row = id / nc;
            int col = id % nc;
            if (row - 1 >= 0 && grid[row-1][col] == ‘1‘) {
              nei***ors.add((row-1) * nc + col);
              grid[row-1][col] = ‘0‘;
            }
            if (row + 1 < nr && grid[row+1][col] == ‘1‘) {
              nei***ors.add((row+1) * nc + col);
              grid[row+1][col] = ‘0‘;
            }
            if (col - 1 >= 0 && grid[row][col-1] == ‘1‘) {
              nei***ors.add(row * nc + col-1);
              grid[row][col-1] = ‘0‘;
            }
            if (col + 1 < nc && grid[row][col+1] == ‘1‘) {
              nei***ors.add(row * nc + col+1);
              grid[row][col+1] = ‘0‘;
            }
          }
        }
      }
    }

    return num_islands;
  }
}

https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-shu-liang-by-leetcode/

原文地址:https://www.cnblogs.com/twoheads/p/11476955.html

时间: 2024-08-30 09:03:56

【leetcode-200 深度优先+广度优先】 岛屿数量的相关文章

Leetcode 200.岛屿的数量 - DFS、BFS

Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7MB,99.7% / 92% public: /** * row,col: 坐标,以0开始. * 用row和col,而不是x,y. 否则容易写成grid[x][y],顺序不对!! */ void dfs_set_zero(vector<vector<char>>& grid, i

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or v

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合. 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系.如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道.你必须输出所有学生

[LeetCode]小工具,统计数量,隐藏上锁的题目

LeetCode Problems List没有统计数量的功能,顺手写了一个. 下面两段只是用jquery调整网页上显示的内容,刷新网页就没用了. 比如想看题目里一共有多少Easy, Medium和Hard,就在浏览器Console中运行下面的代码. 还有一种用法,比如看还有多少没做,或者没做的题中有多少Easy, Medium和Hard,先选Unsolved Problems的filter,再用控制台运行该代码. 1 //Count Problems 2 var list = $("tbody

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

[LeetCode] 200. Number of Islands 岛屿的数量

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

leetcode 200. 岛屿数量(dfs||bfs)

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入:11110110101100000000 输出: 1示例 2: 输入:11000110000010000011 输出: 3 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/number-of-islands class Solution { pu

LeetCode | 200. 岛屿数量

原题(Medium): 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 思路:递归 这题类型跟我们之前做过的一题单词搜索类似.所以我在看到这题之后,就想到了可以用与之相同的思路:建立一个代表搜索方向的数组,在当前坐标下,如果往某个方向移动一格是符合一定条件的,就往该方向移动一格,且是递归形式的移动.在这里条件就是不超越边界,且该方向上有岛屿可供移动,且是未被访问过

DFS或BFS(深度优先搜索或广度优先搜索遍历无向图)-04-无向图-岛屿数量

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入:11110110101100000000 输出: 1示例 2: 输入:11000110000010000011 输出: 3 1 class Solution { 2 public: 3 int dist[4][2] = {0,1,0,-1,1,0,-1,0}; 4 queue<pair<int,i