图论第三天

似乎要团建了,我再猫会。我必须参与上团建再走。

130.被围绕的区域

先把外围的O变成A,再把飞地的O变成X,再把外围A变回O

cpp 复制代码
class Solution {
public:
    int neighbor[4][2] ={1,0,0,-1,-1,0,0,1};
    void solve(vector<vector<char>>& board) {
        queue<pair<int,int>>que;
        int n = board.size();
        int m = board[0].size();
        for(int i = 0;i < n;i++){
            if(board[i][0] == 'O')bfs(board,que,i,0,'O','A');
            if(board[i][m-1] == 'O')bfs(board,que,i,m-1,'O','A');
        }
        for(int j = 0;j < m;j++){
            if(board[0][j] == 'O')bfs(board,que,0,j,'O','A');
            if(board[n-1][j] == 'O')bfs(board,que,n-1,j,'O','A');
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(board[i][j] == 'O')bfs(board,que,i,j,'O','X');
            }
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(board[i][j] == 'A')bfs(board,que,i,j,'A','O');
            }
        }
    }
    void bfs(vector<vector<char>>& board,queue<pair<int,int>>que,int x,int y,char X,char Y){
        que.push({x,y});
        board[x][y] = Y;
        while(!que.empty()){
            pair<int,int>cur = que.front();
            que.pop();
            for(int i = 0;i< 4;i++){
                int nextx = cur.first + neighbor[i][0];
                int nexty = cur.second + neighbor[i][1];
                if(nextx < 0 || nexty < 0 || nextx >= board.size() ||nexty >= board[0].size())continue;
                if(board[nextx][nexty] == X){
                    board[nextx][nexty] = Y;
                    que.push({nextx,nexty});
                }
            }

        }
    }
};

417. 太平洋大西洋水流问题

思路:从两边溯流而上。

对不起!!歇一晚上。明天补上。

相关推荐
贾斯汀玛尔斯8 小时前
每天学一个算法--LSM-Tree(Log-Structured Merge Tree)
java·算法·lsm-tree
浅念-12 小时前
刷穿LeetCode:BFS 解决 Flood Fill 算法
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
做cv的小昊13 小时前
【TJU】研究生应用统计学课程笔记(8)——第四章 线性模型(4.1 一元线性回归分析)
笔记·线性代数·算法·数学建模·回归·线性回归·概率论
贾斯汀玛尔斯14 小时前
每天学一个算法--倒排索引(Inverted Index)
算法·inverted-index
小e说说14 小时前
打破偏科困境:这些学习软件助孩子重燃学习热情
算法
月昤昽14 小时前
autoCAD二次开发 4.正多边形与collection区分
算法·c#·二次开发·autocad二次开发
休息一下接着来14 小时前
C++ 固定容量环形队列实现
c++·算法
im_AMBER15 小时前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵
笨笨饿15 小时前
#79_NOP()嵌入式C语言中内联汇编宏的抽象封装模式研究
linux·c语言·网络·驱动开发·算法·硬件工程·个人开发