LeetCode //C - 200. Number of Islands

200. Number of Islands

Given an m x n 2D binary grid grid which represents a map of *'1'*s (land) and *'0'*s (water), return 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 water.

Example 1:

Input: grid = [

["1","1","1","1","0"],

["1","1","0","1","0"],

["1","1","0","0","0"],

["0","0","0","0","0"]

]
Output: 1

Example 2:

Input: grid = [

["1","1","0","0","0"],

["1","1","0","0","0"],

["0","0","1","0","0"],

["0","0","0","1","1"]

]
Output: 3

Constraints:
  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is '0' or '1'.

From: LeetCode

Link: 200. Number of Islands


Solution:

Ideas:

1. Initialize: The numIslands function initializes the count of islands to zero. It also determines the dimensions of the grid, m (number of rows) and n (number of columns).

2. Iterate through the Grid: The code then iterates over each cell of the grid. If the current cell is '1', it indicates a part of an island that hasn't been explored yet.

3. DFS to Explore the Island: When a '1' is found, we use the dfs function to explore the entire island. The idea is that once we find a '1', we want to explore all its neighboring cells (up, down, left, and right) to see if they are also part of the same island. If they are, we continue the exploration recursively.

  • In the dfs function, if the current cell is out-of-bounds, or if it is water (i.e., '0'), we return immediately without further exploration.
  • If the current cell is '1', we mark it as visited by changing its value to '0'. This ensures that we don't count the same part of an island more than once.
  • We then recursively call dfs on the neighboring cells to continue exploring the island.

4. Counting the Islands: Each time we initiate a DFS exploration from the numIslands function (i.e., every time we find an unexplored '1'), we increment our island count by 1. By the end of the iteration over the grid, we would have explored and counted all distinct islands.

5. Return the Count: Finally, numIslands returns the count of islands.

Code:
c 复制代码
void dfs(char** grid, int i, int j, int m, int n) {
    if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') {
        return;
    }

    grid[i][j] = '0';
    
    dfs(grid, i - 1, j, m, n); // up
    dfs(grid, i + 1, j, m, n); // down
    dfs(grid, i, j - 1, m, n); // left
    dfs(grid, i, j + 1, m, n); // right
}

int numIslands(char** grid, int gridSize, int* gridColSize) {
    if (grid == NULL || gridSize == 0 || gridColSize == NULL) {
        return 0;
    }

    int m = gridSize;
    int n = gridColSize[0];
    int islandCount = 0;
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (grid[i][j] == '1') {
                islandCount++;
                dfs(grid, i, j, m, n);
            }
        }
    }

    return islandCount;
}
相关推荐
汉克老师1 小时前
GESP2024年3月认证C++六级( 第三部分编程题(1)游戏)
c++·学习·算法·游戏·动态规划·gesp6级
闻缺陷则喜何志丹1 小时前
【C++图论】2685. 统计完全连通分量的数量|1769
c++·算法·力扣·图论·数量·完全·连通分量
利刃大大2 小时前
【二叉树深搜】二叉搜索树中第K小的元素 && 二叉树的所有路径
c++·算法·二叉树·深度优先·dfs
CaptainDrake2 小时前
力扣 Hot 100 题解 (js版)更新ing
javascript·算法·leetcode
一缕叶2 小时前
洛谷P9420 [蓝桥杯 2023 国 B] 子 2023 / 双子数
算法·蓝桥杯
甜甜向上呀2 小时前
【数据结构】空间复杂度
数据结构·算法
Great Bruce Young3 小时前
GPS信号生成:C/A码序列生成【MATLAB实现】
算法·matlab·自动驾驶·信息与通信·信号处理
Mryan20053 小时前
LeetCode | 不同路径
数据结构·c++·算法·leetcode
qy发大财3 小时前
验证二叉搜索树(力扣98)
数据结构·算法·leetcode·职场和发展
人类群星闪耀时4 小时前
用深度学习优化供应链管理:让算法成为商业决策的引擎
人工智能·深度学习·算法