LeetCode //C - 130. Surrounded Regions

130. Surrounded Regions

Given an m x n matrix board containing 'X' and 'O' , capture all regions that are 4-directionally surrounded by 'X'.

A region is captured by flipping all 'O' s into 'X' s in that surrounded region.

Example 1:

Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Explanation: Notice that an 'O' should not be flipped if:

  • It is on the border, or
  • It is adjacent to an 'O' that should not be flipped.

The bottom 'O' is on the border, so it is not flipped.

The other three 'O' form a surrounded region, so they are flipped.

Example 2:

Input: board = [["X"]]
Output: [["X"]]

Constraints:
  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 200
  • board[i][j] is 'X' or 'O'.

From: LeetCode

Link: 130. Surrounded Regions


Solution:

Ideas:
  1. Iterate over the boundary (four sides) of the board.
  2. For every 'O' on the boundary, perform a Depth First Search (DFS) to mark all 'O's connected to it with a temporary marker, such as 'B', to denote that these 'O's are on the boundary or connected to the boundary and should not be flipped.
  3. After marking all 'O's on the boundary and the ones connected to it, iterate over the entire board. Perform the following operations:
    • Change all 'B' to 'O' as these are not surrounded by 'X'.
    • Change all remaining 'O' to 'X' as these are surrounded by 'X'.
Code:
c 复制代码
void dfs(char** board, int i, int j, int m, int n) {
    if(i < 0 || i >= m || j < 0 || j >= n || board[i][j] != 'O') return;
    
    // mark the current cell as 'B'
    board[i][j] = 'B';
    
    // perform DFS in all four directions
    dfs(board, i+1, j, m, n);
    dfs(board, i-1, j, m, n);
    dfs(board, i, j+1, m, n);
    dfs(board, i, j-1, m, n);
}

void solve(char** board, int boardSize, int* boardColSize) {
    if(boardSize == 0 || boardColSize[0] == 0) return;
    
    int m = boardSize, n = boardColSize[0];
    
    // Step 1: mark the boundary 'O's and the ones connected to them with 'B'
    for(int i = 0; i < m; i++) {
        dfs(board, i, 0, m, n); // left boundary
        dfs(board, i, n-1, m, n); // right boundary
    }
    
    for(int j = 0; j < n; j++) {
        dfs(board, 0, j, m, n); // top boundary
        dfs(board, m-1, j, m, n); // bottom boundary
    }
    
    // Step 2: flip the remaining 'O's to 'X' and 'B's back to 'O'
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            if(board[i][j] == 'O') board[i][j] = 'X';
            if(board[i][j] == 'B') board[i][j] = 'O';
        }
    }
}
相关推荐
wuqingshun31415912 分钟前
蓝桥杯 11. 打印大X
数据结构·算法·职场和发展·蓝桥杯·深度优先
Blossom.1181 小时前
量子网络:构建未来通信的超高速“高速公路”
网络·opencv·算法·安全·机器学习·密码学·量子计算
A林玖1 小时前
【机器学习】朴素贝叶斯
人工智能·算法·机器学习
六边形战士DONK1 小时前
神经网络基础[损失函数,bp算法,梯度下降算法 ]
人工智能·神经网络·算法
wuqingshun3141592 小时前
蓝桥杯 2. 确定字符串是否是另一个的排列
数据结构·c++·算法·职场和发展·蓝桥杯
小刘|2 小时前
JVM 自动内存管理
java·jvm·算法
小羊不会c++吗(黑客小羊)2 小时前
c++头文件知识
算法
拓端研究室TRL2 小时前
PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究
大数据·人工智能·python·算法·机器学习
CoovallyAIHub3 小时前
Vision Transformers与卷积神经网络详细训练对比(附代码)
深度学习·算法·计算机视觉
地平线开发者3 小时前
征程 6 逆向自证hbm与bc一致性
算法·自动驾驶