LeetCode200.岛屿数量

在这里插入图片描述


LeetCode200.岛屿数量


题目:

  • 给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
  • 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
  • 此外,你可以假设该网格的四条边均被水包围。

示例1:

  • 输入: grid = [[“1”,“1”,“1”,“1”,“0”],
          [“1”,“1”,“0”,“1”,“0”],
          [“1”,“1”,“0”,“0”,“0”],
          [“0”,“0”,“0”,“0”,“0”]]
  • 输出: 1 1 1

示例2:

  • 输入: grid = [[“1”,“1”,“0”,“0”,“0”],
          [“1”,“1”,“0”,“0”,“0”],
          [“0”,“0”,“1”,“0”,“0”],
          [“0”,“0”,“0”,“1”,“1”]]
  • 输出: 3 3 3

解题思路:(BFS)
用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。

C++版整体代码(BFS)一:
定义一个二维bool数组,用来记录节点是否被访问过。然后执行BFS操作。

class Solution {
public:
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y){
        queue<pair<int, int>> q;
        q.push({x, y});
        while(!q.empty()){
            pair<int, int> cur = q.front();
            q.pop();
            int curx = cur.first;
            int cury = cur.second;
            for(int i = 0; i < 4; i++){
                int nextx = curx + dx[i], nexty = cury + dy[i];
                if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
                if(!visited[nextx][nexty] && grid[nextx][nexty] == '1'){
                    q.push({nextx, nexty});
                    visited[nextx][nexty] = true;
                } 
            }
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> visited = vector<vector<bool>>(n, vector<bool>(m, false));
        int res = 0;
        for(int i = 0; i < grid.size(); i++){
            for(int j = 0; j < grid[0].size(); j++){
                if(!visited[i][j] && grid[i][j] == '1'){
                    res++;
                    bfs(grid, visited, i, j);
                }
            }
        } 
        return res;
    }
};

C++版整体代码二(BFS)(简洁版):
遍历grid,若值等于'1'则加入队列中,并将其置为'2'或其他任意数,代表已访问过。之后的遍历过程中便不会再次访问该节点。

class Solution {
public:
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    int numIslands(vector<vector<char>>& grid) {
        int cnt = 0;
        for(int i = 0; i < grid.size(); i++){
            for(int j = 0; j < grid[0].size(); j++){
                if(grid[i][j] == '1'){
                    cnt++;
                    //      ------ bfs --------
                    grid[i][j] = '2';
                    queue<pair<int, int>> q;
                    q.push({i, j});
                    while(!q.empty()){
                        auto t = q.front();
                        q.pop();
                        int curx = t.first, cury = t.second;
                        for(int k = 0; k < 4; k++){
                            int nextx = curx + dx[k], nexty = cury + dy[k];
                            if(nextx >= 0 && nextx < grid.size() && nexty >= 0 && nexty < grid[0].size() && grid[nextx][nexty] == '1'){
                                q.push({nextx, nexty});
                                grid[nextx][nexty] = '2';
                            }
                        }
                    }
                }
            }
        }
        return cnt;
    }
};

C++版整体代码三(DFS):

class Solution {
public:
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y){
        for(int i = 0; i < 4; i++){
            int nextx = x + dx[i];
            int nexty = y + dy[i];
            if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
            if(!visited[nextx][nexty] && grid[nextx][nexty] == '1'){
                visited[nextx][nexty] = true;
                dfs(grid, visited, nextx, nexty);
            }
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> visited = vector<vector<bool>>(n, vector<bool>(m, false));
        int res = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0 ; j < m; j++){
                if(!visited[i][j] && grid[i][j] == '1'){
                    visited[i][j] = true;
                    res++;
                    dfs(grid, visited, i, j);
                }
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值