岛屿数量

力扣地址

深度优先搜索

用 record 记录数量

两层 for 循环开始遍历

找到一个为 1 的值后,将 record 加 1,并将对当前值进行深度优先搜索,

将周围所有为 1 的值全部置为 0。

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty() || grid[0].empty()) return 0;
        int rows = grid.size();
        int cols = grid[0].size();
        int record = 0;
        
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (grid[i][j] == '1') {
                    ++record;
                    dfs(grid, rows, cols, i, j);
                }
            }
        }
        return record;
    }

    void dfs(vector<vector<char>>& grid, int rows, int cols, int i, int j) {
        grid[i][j] = '0';
        if (i + 1 < rows && grid[i + 1][j] == '1') dfs(grid, rows, cols, i + 1, j);
        if (i - 1 >= 0 && grid[i - 1][j] == '1') dfs(grid, rows, cols, i - 1, j);
        if (j - 1 >= 0 && grid[i][j - 1] == '1') dfs(grid, rows, cols, i, j - 1);
        if (j + 1 < cols && grid[i][j + 1] == '1') dfs(grid, rows, cols, i, j + 1);
    }
};

广度优先搜索

用 record 记录数量

两层 for 循环开始遍历

找到一个为 1 的值后,将 record 加 1,并将当前值加入队列进行广度优先搜索,

要在入队前将值置为 0,否则会围绕一个点产生无限循环

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty() || grid[0].empty()) return 0;
        int rows = grid.size();
        int cols = grid[0].size();
        int record = 0;
        std::queue<std::pair<int, int>> qpair;
        
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (grid[i][j] == '1') {
                    qpair.push({i, j});
                    grid[i][j] = '0';
                    ++record;
                    while (!qpair.empty()) {
                        int row = qpair.front().first;
                        int col = qpair.front().second;
                        qpair.pop();
                        if (row + 1 < rows && grid[row + 1][col] == '1') {
                            grid[row + 1][col] = '0';
                            qpair.push({row + 1, col});
                        }
                        if (row - 1 >= 0 && grid[row - 1][col] == '1') {
                            grid[row - 1][col] = '0';
                            qpair.push({row - 1, col});
                        }
                        if (col - 1 >= 0 && grid[row][col - 1] == '1') {
                            grid[row][col - 1] = '0';
                            qpair.push({row, col - 1});
                        }
                        if (col + 1 < cols && grid[row][col + 1] == '1') {
                            grid[row][col + 1] = '0';
                            qpair.push({row, col + 1});
                        }
                    }
                }
            }
        }
        return record;
    }
};

并查集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值