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;
}
相关推荐
88号技师2 小时前
2024年12月一区SCI-加权平均优化算法Weighted average algorithm-附Matlab免费代码
人工智能·算法·matlab·优化算法
IT猿手2 小时前
多目标应用(一):多目标麋鹿优化算法(MOEHO)求解10个工程应用,提供完整MATLAB代码
开发语言·人工智能·算法·机器学习·matlab
88号技师2 小时前
几款性能优秀的差分进化算法DE(SaDE、JADE,SHADE,LSHADE、LSHADE_SPACMA、LSHADE_EpSin)-附Matlab免费代码
开发语言·人工智能·算法·matlab·优化算法
Zer0_on2 小时前
数据结构栈和队列
c语言·开发语言·数据结构
一只小bit2 小时前
数据结构之栈,队列,树
c语言·开发语言·数据结构·c++
马浩同学3 小时前
【GD32】从零开始学GD32单片机 | DAC数模转换器 + 三角波输出例程
c语言·单片机·嵌入式硬件·mcu
我要学编程(ಥ_ಥ)3 小时前
一文详解“二叉树中的深搜“在算法中的应用
java·数据结构·算法·leetcode·深度优先
埃菲尔铁塔_CV算法3 小时前
FTT变换Matlab代码解释及应用场景
算法
一个没有本领的人3 小时前
win11+matlab2021a配置C-COT
c语言·开发语言·matlab·目标跟踪
一只自律的鸡3 小时前
C项目 天天酷跑(下篇)
c语言·开发语言