图论第2天----第1020题、第130题

图论第2天----第1020题、第130题

文章目录

​ 又继续开始修行,把图论这块补上,估计要个5-6天时间。

一、第1020题--飞地的数量

​ 跟前面做的思路差不多,其实有另外一种思路。我这里是在遍历每块岛屿时,判断是否四周都是水,进而再统计数量。另一种思路,与130思路差不多,从边上开始遍历,要遍历2遍。

c++ 复制代码
class Solution {
public:
    int count;
    int result = 0;
    bool flag;
    int dir[4][2] = {0,1,1,0,0,-1,-1,0};

    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()){
                flag = true;
                continue;
            }
            if(!visited[nextx][nexty] && grid[nextx][nexty] == 1){
                visited[nextx][nexty] = true;
                count++;
                dfs(grid, visited, nextx, nexty);
            }
        }
    }

    int numEnclaves(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<bool>> visited(n, vector<bool>(m, false));
        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;
                    flag = false;
                    count = 1;
                    dfs(grid, visited, i, j);
                    cout << flag << ' ' << count << endl;
                    if (!flag) result += count;
                }
            }
        }
        return result;
    }
};

二、第130题--被围绕的区域

​ 遍历2遍。第一遍从边上开始遍历,把边上的岛屿做标记,记为'A'。这样边上的岛屿('A')与中间的岛屿('O')区分开了。第二遍遍历时,每个元素都遍历到,把'O'变为'X',把'A'变为'O',就把边上边上的岛屿留下了。

c++ 复制代码
class Solution {
public:
    int dir[4][2] = {0,1,1,0,0,-1,-1,0};
    void dfs(vector<vector<char>>& board, int x, int y){
        board[x][y] = 'A';
        for(int i=0; i<4; i++){
            int nextx = x + dir[i][0];
            int nexty = y + dir[i][1];
            if(nextx <0 || nexty <0 || nextx >=board.size() || nexty >=board[0].size()) continue;
            if(board[nextx][nexty] == 'X' || board[nextx][nexty] == 'A') continue;
            dfs(board, nextx, nexty);
        }
    }

    void solve(vector<vector<char>>& board) {
        int n = board.size();
        int m = board[0].size();
        for(int i=0; i<n; i++){
            if(board[i][0] == 'O') dfs(board, i, 0);
            if(board[i][m-1] == 'O') dfs(board, i, m-1);
        } 
        for(int j=0; j<m; j++){
            if(board[0][j] == 'O') dfs(board, 0, j);
            if(board[n-1][j] == 'O') dfs(board, n-1, j);
        }
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(board[i][j] == 'O') board[i][j] = 'X';
                if(board[i][j] == 'A') board[i][j] = 'O';
            }
        }
    }
};
相关推荐
go546315846510 分钟前
修改Spatial-MLLM项目,使其专注于无人机航拍视频的空间理解
人工智能·算法·机器学习·架构·音视频·无人机
油泼辣子多加28 分钟前
【Torch】nn.BatchNorm1d算法详解
算法
nlog3n28 分钟前
基于 govaluate 的监控系统中,如何设计灵活可扩展的自定义表达式函数体系
算法·go
IT古董40 分钟前
【第三章:神经网络原理详解与Pytorch入门】01.神经网络算法理论详解与实践-(2)神经网络整体结构
pytorch·神经网络·算法
ThetaarSofVenice1 小时前
垃圾收集相关算法Test
java·jvm·算法
小陈phd1 小时前
langchain从入门到精通(二十八)——RAG优化策略(六)集成多种检索器算法实现混合检索及问题转换总结
算法
是小王同学啊~1 小时前
(LangChain)RAG系统链路向量检索器之Retrievers(五)
python·算法·langchain
薰衣草23331 小时前
一天两道力扣(1)
算法·leetcode·职场和发展
一粒沙白猫1 小时前
Java综合练习04
java·开发语言·算法
爱coding的橙子2 小时前
每日算法刷题Day41 6.28:leetcode前缀和2道题,用时1h20min(要加快)
算法·leetcode·职场和发展