Leetcode热题100-200 岛屿数量

Leetcode热题100-200 岛屿数量

  • [1. 题目描述](#1. 题目描述)
  • [2. 代码实现](#2. 代码实现)
    • [1. dfs算法](#1. dfs算法)
    • [2. bfs算法](#2. bfs算法)

1. 题目描述

200 岛屿数量

2. 代码实现

1. dfs算法

cpp 复制代码
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int res = 0;

        // 注意lambda函数本身是不支持递归的,需要捕获自身来实现递归调用
        // 使用 lambda 表达式实现 DFS
        auto dfs = [&](auto&& dfs, int x, int y) -> void {
            // 超出边界检查
            if (x < 0 || x >= m || y < 0 || y >= n)
                return;

            // 不为陆地或者已访问过
            if (grid[x][y] != '1')
                return;

            // 设置为已访问
            grid[x][y] = '0';

            // 向四个方向递归
            dfs(dfs, x - 1, y); // 上
            dfs(dfs, x + 1, y); // 下
            dfs(dfs, x, y - 1); // 左
            dfs(dfs, x, y + 1); // 右
        };
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    dfs(dfs, i, j);
                    res += 1;
                }
            }
        }
        return res;
    }
};

2. bfs算法

cpp 复制代码
class Solution {
public:
    // 定义方向向量:上下左右
    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int res = 0;

        // 使用bfs遍历的方式来实现
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    queue<pair<int, int>> que;
                    que.push({i, j});
                    // 标记为已访问
                    grid[i][j] = '0';

                    while (!que.empty()) {
                        auto [x, y] = que.front();
                        que.pop();

                        // 遍历四个方向
                        for (const auto& [dx, dy] : dirs) {
                            int new_x = x + dx;
                            int new_y = y + dy;

                            // 判断是否越界以及是否为陆地
                            if (new_x >= 0 && new_x < m && new_y >= 0 &&
                                new_y < n && grid[new_x][new_y] == '1') {
                                que.push({new_x, new_y});
                                grid[new_x][new_y] = '0';
                            }
                        }
                    }
                    res++;
                }
            }
        }
        return res;
    }
};
相关推荐
yzx99101311 分钟前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题14 分钟前
算法练习-回溯
算法
眼镜哥(with glasses)1 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
zh_xuan4 小时前
c++ 单例模式
开发语言·c++·单例模式
int型码农6 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
利刃大大6 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
UFIT6 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面6 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked936 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,6 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法