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';
        }
    }
}
相关推荐
随意起个昵称2 小时前
区间dp-基础题目1(石子合并)
算法·动态规划
吞下星星的少年·-·3 小时前
线段树模板
算法
段一凡-华北理工大学3 小时前
2026 高炉炼铁智能化技术全景与演进路径~系列文章11:演进路径与行业未来
大数据·网络·人工智能·算法·工业智能体·高炉炼铁智能化
叶小鸡3 小时前
小鸡玩算法-力扣HOT100-多维动态规划
算法·leetcode·动态规划
星马梦缘4 小时前
aaaaa
数据结构·c++·算法
菜菜的顾清寒4 小时前
力扣HOT100(42)链表-随机链表的复制
算法·leetcode·链表
lqqjuly4 小时前
模型剪枝与稀疏化:理论、算法与可运行实现
人工智能·算法·剪枝
逻辑君5 小时前
Foresight研究报告【20260011】
人工智能·线性代数·算法·矩阵
珊瑚里的鱼5 小时前
【动态规划】不同路径Ⅱ
算法·动态规划
星恒随风5 小时前
C语言数据结构排序算法详解(下):冒泡排序、快速排序、归并排序和计数排序
c语言·数据结构·笔记·学习·排序算法