Leetcode problem solution-130. Surrounded area

Topic link

Title description:

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

After running your function, the matrix becomes:

X X X X
X X X X
X X X X
X O X X

Explanation:

Surrounded interval does not exist on the boundary, in other words, on any boundary 'O'will not be filled 'X'. Not on any border, and on the boundary or not 'O'connected to 'O'eventually be filled 'X'. If two elements are adjacent in the horizontal or vertical direction, they are said to be "connected".


Problem-solving ideas

  To read the question carefully, the question can actually be simplified into one sentence: Find the connected area of ​​the boundary O. To put it bluntly is to judge whether there is O on the boundary. If it does, it will spread in four directions from this point O. The O in its direction cannot be changed until it meets X or reaches another boundary.
  At the beginning, I wanted to directly find O in the four directions in the double loop traversal once and for all, ignoring the O in these four directions even if it belongs to the internal node of the node (that is, the boundary of the traversal node is X in all directions) , But it does not necessarily belong to the internal node of its own direction, and the result falls in test case 31.
  Later, inspired by the great gods of the problem solution area, you can first find the position where the boundary is O, and then DFS find the connected area of ​​O in the four directions, and set the value of the area to F (any value other than X and O can be used. ), the remaining O must be an internal node, and the final double-loop reduction is enough.


Code

class Solution {
    
    
public:
    void solve(vector<vector<char>>& board) {
    
    
        if(board.size() <= 2)
            return;

        int row = board.size(), line = board[0].size();
        for(int i = 0; i < row; i++){
    
    
            for(int j = 0; j < line; j++){
    
    
                bool flag = i == 0 || i == row - 1 || j == 0 || j == line - 1;
                if(flag && board[i][j] == 'O')
                    helper(board, i, j);
            }
        }

        for(int i = 0; i < row; i++){
    
    
            for(int j = 0; j < line; j++){
    
    
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == 'F')
                    board[i][j] = 'O';
            }
        }

        return;
    }

private:
    void helper(vector<vector<char>>& board, int i, int j){
    
    
        if(i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] == 'X' || board[i][j] == 'F')
            return;
        
        board[i][j] = 'F';
        helper(board, i - 1, j);
        helper(board, i + 1, j);
        helper(board, i, j - 1);
        helper(board, i, j + 1);
        return;
    }
};

If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/105128895