leetcode130. Surrounded area/dfs, bfs

Subject: 130. Surrounded area

Given a two-dimensional matrix containing'X' and'O' (letter O).

Find all the areas surrounded by'X' and fill all the'O's in these areas with'X'.

Example:

X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:

X X X X
X X X X
X X X X
X O X X
解释:

被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/surrounded-regions
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea: dfs

The key to this question is how to recognize that the area connected by the “O” is connected to the “O” on the boundary.

  • First process the area connected by "O" on the boundary, and mark the area with dfs
  • Finally, traverse the matrix once and set the marked area to "O" and the "O" area to "X"
class Solution {
    
    
public:
    int row, col;
    vector<vector<int>> dir;
    void solve(vector<vector<char>>& board) {
    
    
        //先将边上0相连的区域做上标记
        row = board.size();
        if(row == 0)
            return;
        col = board[0].size();
        dir = {
    
    {
    
    -1, 0}, {
    
    0, -1}, {
    
    0, 1}, {
    
    1, 0}};
        
        //第一行
        for(int i = 0; i < col; ++i){
    
    
            if(board[0][i] == 'O'){
    
    
                dfs(board, 0, i);
            }
        }
        
        //第一列
        for(int i = 1; i < row; ++i){
    
    
            if(board[i][0] == 'O'){
    
    
                dfs(board, i, 0);
            }
        }
        
        //最后一行
        for(int i = 1; i < col; ++i){
    
    
            if(board[row - 1][i] == 'O'){
    
    
                dfs(board, row - 1, i);
            }
        }
        
        //最后一列
        for(int i = 1; i < row - 1; ++i){
    
    
            if(board[i][col - 1] == 'O'){
    
    
                dfs(board, i, col - 1);
            }
        }
        
        //处理最终结果
        for(int i = 0; i < row; ++i){
    
    
            for(int j = 0; j < col; ++j){
    
    
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == 'U')
                    board[i][j] = 'O';
            }
        }
    }
    void dfs(vector<vector<char>>& board, int x, int y){
    
    
        board[x][y] = 'U';
        for(auto d : dir){
    
    
            int cx = d[0] + x;
            int cy = d[1] + y;
            if(judge(cx, cy) && board[cx][cy] == 'O'){
    
    
                dfs(board, cx, cy);
            }
        }
    }
    bool judge(int x, int y){
    
    
        return x >= 0 && x < row && y >= 0 && y < col;
    }
};

Of course, this question can also be obtained with bfs. First, mark all the "O"s on the boundary into the team, mark them when they leave the team, and enter the "O"s in the four adjacent directions.

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/107928702
Recommended