[牛客网-Leetcode] #Array#DFS #Backtracking is harder surrounded-regions

Region surrounded surrounded-regions

Title description

Now there is a two-dimensional board that only contains'X' and'O'. Please capture all the areas surrounded by'X'. The way to
capture an enclosed area is to change all the'O's in the enclosed area to'X' '
For example,
XXXX
XOOX
XXOX
XOXX After
executing the function you give, this two-dimensional board should become:
XXXX
XXXX
XXXX
XOXX

Given a 2D board containing’X’and’O’, capture all regions surrounded by’X’.
A region is captured by flipping all’O’s into’X’s in that surrounded region .

For example,

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

After running your function, the board should be:

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

Problem-solving ideas

  • DFS:
    • All Os connected to the four edges are kept, and all other Os become X
    • Traverse the O on the four edges, and traverse the O connected to it in depth, and turn these Os into *
    • Change the remaining O to X
    • Change the remaining * to O
  • This problem does not need backtracking, because there is no need to record the path , only the value of the element needs to be changed when each position is reached, so DFS can be used to solve it.
  • The relationship between backtracking and DFS:
    The backtracking method is to find the solution of the problem, using DFS (depth first search). In the process of DFS, it is found that the solution is not the problem, then it starts to trace back to the previous layer or the previous node. DFS traverses the entire search space, regardless of whether it is a solution to the problem. So I feel that backtracking is an application of DFS, and DFS is more like a tool.
class Solution {
    
    
public:
    int rowNum, colNum;
    void solve(vector<vector<char>> &board) {
    
    
        if(board.size() == 0 || board[0].size() == 0) return ;
        rowNum = board.size();
        colNum = board[0].size();
        //将与上下两边的O相邻的所有O全变成*
        for(int i = 0; i < colNum; i ++) {
    
    
            dfs(board, 0, i);           //遍历上边的O
            dfs(board, rowNum - 1, i);  //遍历下边的O
        }
		//将与左右两边的O相邻的所有O全变成*
        for(int i = 0; i < rowNum; i ++) {
    
    
            dfs(board, i, 0);           //遍历左边的O
            dfs(board, i, colNum - 1);  //遍历右边的O
        }
        for(int i = 0; i < rowNum; i ++) {
    
    
            for(int j = 0; j < colNum; j ++) {
    
    
                if(board[i][j] == '*') {
    
    
                    board[i][j] = 'O';
                } else if(board[i][j] == 'O') {
    
    
                    board[i][j] = 'X';
                }
            }
        }
    }
    //将坐标为row,col上的O相邻的所有O全变成*
    void dfs(vector<vector<char>> &board, int row, int col) {
    
    
        if(board[row][col] == 'O') {
    
    
            board[row][col] = '*';
            if(row > 0) dfs(board, row - 1, col);  //往上
            if(row < rowNum - 1) dfs(board, row + 1, col);  //往下
            if(col > 0) dfs(board, row, col - 1);  //往左
            if(col < colNum - 1) dfs(board, row, col + 1);  //往右
        }
    }
    
};

Guess you like

Origin blog.csdn.net/cys975900334/article/details/106680927