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 == boardi.length
  • 1 <= m, n <= 200
  • boardij 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';
        }
    }
}
相关推荐
ZhengEnCi13 分钟前
一亿条数据取前100条完全指南:从全量排序到分块流式最小堆的4种解法实测
python·算法·排序算法
C++ 老炮儿的技术栈18 分钟前
机器人的坐标,像 XYZ 的坐标值,是用单精度浮点还是用双精度浮点?
c语言·c++·人工智能·单片机·c
zaizai100720 分钟前
STL深度剖析2
java·c++·算法
数智奔流32 分钟前
MiniMax的低价攻势,撞上“背题”疑云
前端·javascript·算法
青少儿编程课堂32 分钟前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:位运算实战——二进制权限与最大异或解析
c++·python·算法·bfs·信息学竞赛
广州智造34 分钟前
Radioss中破坏模型FAILTAB1的详细介绍
数据库·人工智能·算法·机器学习·仿真软件·radioss
青少儿编程课堂35 分钟前
2026蓝桥杯青少组图形化真题:校园飞机大战解析(全国青少年信息素养·电子学会图形化考级)
c++·python·算法·bfs·信息学竞赛
开开心心就好41 分钟前
证件照排版打印工具,免费可自定义尺寸
服务器·开发语言·算法·随机森林·pdf·音视频·散列表
lisw0543 分钟前
导数与机器学习优化!
算法·机器学习·数学建模
海绵宝宝转agent1 小时前
leetcodehot100个人题解
java·笔记·学习·算法