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';
        }
    }
}
相关推荐
好奇龙猫2 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸3 小时前
链表的归并排序
数据结构·算法·链表
jrrz08283 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time3 小时前
golang学习2
算法
南宫生4 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步5 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
DARLING Zero two♡5 小时前
关于我、重生到500年前凭借C语言改变世界科技vlog.16——万字详解指针概念及技巧
c语言·开发语言·科技
Ni-Guvara5 小时前
函数对象笔记
c++·算法
泉崎5 小时前
11.7比赛总结
数据结构·算法