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';
        }
    }
}
相关推荐
滴滴答滴答答18 分钟前
LeetCode Hot100 之 17 合并区间
算法·leetcode·职场和发展
你怎么知道我是队长20 分钟前
C语言---排序算法8---递归快速排序法
c语言·算法·排序算法
白太岁26 分钟前
操作系统开发:(8) 任务/线程的创建、调度与管理(实现 tasks.h 与 tasks.c)
c语言·开发语言·bash
007张三丰44 分钟前
软件测试专栏(5/20):自动化测试入门指南:从零开始构建你的第一个测试框架
自动化测试·python·算法·压力测试·测试框架
cameron_tt1 小时前
定时器中断应用 HC-SR04超声波测距模块、定时器输出PWM应用 控制SG90舵机
c语言·嵌入式硬件
Zachery Pole1 小时前
根据高等代数与数分三计算线性回归中的w
算法·回归·线性回归
得一录1 小时前
星图·全参数调试qwen3.1-B
深度学习·算法·aigc
yyjtx1 小时前
DHU上机打卡D22
算法
plus4s1 小时前
2月14日(76-78题)
c++·算法·图论
pzx_0011 小时前
【论文阅读】Attention Is All You Need
论文阅读·算法