130. Surrounded Regions Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Union Find#Matrix



1/**
2 * [130] Surrounded Regions
3 *
4 * Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'.
5 * A region is captured by flipping all 'O's into 'X's in that surrounded region.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" style="width: 550px; height: 237px;" />
9 * Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
10 * Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
11 * Explanation: Notice that an 'O' should not be flipped if:
12 * - It is on the border, or
13 * - It is adjacent to an 'O' that should not be flipped.
14 * The bottom 'O' is on the border, so it is not flipped.
15 * The other three 'O' form a surrounded region, so they are flipped.
16 * 
17 * Example 2:
18 * 
19 * Input: board = [["X"]]
20 * Output: [["X"]]
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	m == board.length
26 * 	n == board[i].length
27 * 	1 <= m, n <= 200
28 * 	board[i][j] is 'X' or 'O'.
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/surrounded-regions/
34// discuss: https://leetcode.com/problems/surrounded-regions/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn solve(board: &mut Vec<Vec<char>>) {
40        
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_130() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.