• 图论第二天|岛屿数量.深搜版、岛屿数量.广搜版、岛屿的最大面积、1020.飞地的数量


    岛屿数量.深搜

    文档讲解 :代码随想录 - 岛屿数量.深搜版
    状态:开始学习。

    本题是dfs模板题

    本题代码:

    class Solution {
    private:
        int dir[4][2] = {0, 1, 1, 0, -1, 0, 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 + dir[i][0];
                int nexty = y + dir[i][1];
                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);
                } 
            }
        }
    public:
        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 result = 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;
                        result++; // 遇到没访问过的陆地,+1
                        dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
                    }
                }
            }
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    岛屿数量.广搜

    文档讲解 :代码随想录 - 岛屿数量.广搜版
    状态:开始学习。

    思路:bfs模板题
    岛屿数量广搜
    本题代码:

    class Solution {
    private:
    int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
    void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
        queue<pair<int, int>> que;
        que.push({x, y});
        visited[x][y] = true; // 只要加入队列,立刻标记
        while(!que.empty()) {
            pair<int ,int> cur = que.front(); que.pop();
            int curx = cur.first;
            int cury = cur.second;
            for (int i = 0; i < 4; i++) {
                int nextx = curx + dir[i][0];
                int nexty = cury + dir[i][1];
                if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 越界了,直接跳过
                if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') {
                    que.push({nextx, nexty});
                    visited[nextx][nexty] = true; // 只要加入队列立刻标记
                }
            }
        }
    }
    public:
        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 result = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (!visited[i][j] && grid[i][j] == '1') {
                        result++; // 遇到没访问过的陆地,+1
                        bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
                    }
                }
            }
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    岛屿的最大面积

    文档讲解 :代码随想录 - 岛屿的最大面积
    状态:开始学习。

    思路:
    这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上**“1”的数量,然后取一个最大**的。

    本题代码(dfs):

    class Solution {
    private:
        int count;
        int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
        void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
            for (int i = 0; i < 4; i++) {
                int nextx = x + dir[i][0];
                int nexty = y + dir[i][1];
                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;
                    count++;
                    dfs(grid, visited, nextx, nexty);
                }
            }
        }
    
    public:
        int maxAreaOfIsland(vector<vector<int>>& grid) {
            int n = grid.size(), m = grid[0].size();
            vector<vector<bool>> visited = vector<vector<bool>>(n, vector<bool>(m, false));
    
            int result = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (!visited[i][j] && grid[i][j] == 1) {
                        count = 1;  // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地
                        visited[i][j] = true;
                        dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
                        result = max(result, count);
                    }
                }
            }
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    本题代码(bfs):

    class Solution {
    private:
        int count;
        int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
        void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
            queue<int> que;
            que.push(x);
            que.push(y);
            visited[x][y] = true; // 加入队列就意味节点是陆地可到达的点
            count++;
            while(!que.empty()) {
                int xx = que.front();que.pop();
                int yy = que.front();que.pop();
                for (int i = 0 ;i < 4; i++) {
                    int nextx = xx + dir[i][0];
                    int nexty = yy + dir[i][1];
                    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;
                        count++;
                        que.push(nextx);
                        que.push(nexty);
                    }
                }
            }
        }
    
    public:
        int maxAreaOfIsland(vector<vector<int>>& grid) {
            int n = grid.size(), m = grid[0].size();
            vector<vector<bool>> visited = vector<vector<bool>>(n, vector<bool>(m, false));
            int result = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (!visited[i][j] && grid[i][j] == 1) {
                        count = 0;
                        bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
                        result = max(result, count);
                    }
                }
            }
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    1020. 飞地的数量

    文档讲解 :代码随想录 - 1020. 飞地的数量
    状态:开始学习。

    思路:
    本题要求找到不靠边的陆地面积,那么我们只要从周边找到陆地然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地都变成海洋,然后再去重新遍历地图的时候,统计此时还剩下的陆地。

    1. 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色
      飞地的数量1
    2. 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样:飞地的数量2
    3. 然后我们再去遍历这个地图,遇到有陆地的地方,去采用dfs或者bfs,统计所有陆地。

    本题代码(dfs):

    class Solution {
    private:
        int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向
        int count; // 统计符合题目要求的陆地空格数量
        void dfs(vector<vector<int>>& grid, int x, int y) {
            grid[x][y] = 0;
            count++;
            for (int i = 0; i < 4; i++) { // 向四个方向遍历
                int nextx = x + dir[i][0];
                int nexty = y + dir[i][1];
                // 超过边界
                if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
                // 不符合条件,不继续遍历
                if (grid[nextx][nexty] == 0) continue;
    
                dfs (grid, nextx, nexty);
            }
            return;
        }
    
    public:
        int numEnclaves(vector<vector<int>>& grid) {
            int n = grid.size(), m = grid[0].size();
            // 从左侧边,和右侧边 向中间遍历
            for (int i = 0; i < n; i++) {
                if (grid[i][0] == 1) dfs(grid, i, 0);
                if (grid[i][m - 1] == 1) dfs(grid, i, m - 1);
            }
            // 从上边和下边 向中间遍历
            for (int j = 0; j < m; j++) {
                if (grid[0][j] == 1) dfs(grid, 0, j);
                if (grid[n - 1][j] == 1) dfs(grid, n - 1, j);
            }
            count = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (grid[i][j] == 1) dfs(grid, i, j);
                }
            }
            return count;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    本题代码(bfs):

    class Solution {
    private:
    int count = 0;
    int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
    void bfs(vector<vector<int>>& grid, int x, int y) {
        queue<pair<int, int>> que;
        que.push({x, y});
        grid[x][y] = 0; // 只要加入队列,立刻标记
        count++;
        while(!que.empty()) {
            pair<int ,int> cur = que.front(); que.pop();
            int curx = cur.first;
            int cury = cur.second;
            for (int i = 0; i < 4; i++) {
                int nextx = curx + dir[i][0];
                int nexty = cury + dir[i][1];
                if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 越界了,直接跳过
                if (grid[nextx][nexty] == 1) {
                    que.push({nextx, nexty});
                    count++;
                    grid[nextx][nexty] = 0; // 只要加入队列立刻标记
                }
            }
        }
    
    }
    
    public:
        int numEnclaves(vector<vector<int>>& grid) {
            int n = grid.size(), m = grid[0].size();
            // 从左侧边,和右侧边 向中间遍历
            for (int i = 0; i < n; i++) {
                if (grid[i][0] == 1) bfs(grid, i, 0);
                if (grid[i][m - 1] == 1) bfs(grid, i, m - 1);
            }
            // 从上边和下边 向中间遍历
            for (int j = 0; j < m; j++) {
                if (grid[0][j] == 1) bfs(grid, 0, j);
                if (grid[n - 1][j] == 1) bfs(grid, n - 1, j);
            }
            count = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (grid[i][j] == 1) bfs(grid, i, j);
                }
            }
            return count;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    Python基础(三):PyCharm安装和使用
    在IDEA中如何新建一个web工程
    PaddleSeg数据集的准备
    RT-Thread v5.0.2 发布
    随想录一刷Day43——动态规划
    套接字编程:UDP通信程序编写、套接字接口、字节序转换接口
    OGG|使用 OGG12.3 同步 Oracle 部分表到 Kafka
    【英语阅读】
    健康报告-设计与实现
    vue学习之列表渲染
  • 原文地址:https://blog.csdn.net/weixin_41725662/article/details/132864030