Data Structure & Algorithms
  • 🖌️Unlocking the Power of Algorithms with C#
  • Data Structure
    • Data Structure
    • Big O
    • Array
    • Linked Lists
    • Stacks
    • Queues
    • Hash Tables
    • Trees
    • Graphs
    • Heap Sort
    • ParkingLot Algorithm
    • LRU cache
    • Priority Queue
  • Algorithms
    • Algorithm
    • Recursion
    • Sorting
    • Searching
    • Dynamic Programming
  • Problems
    • Array
      • Two Sum
      • Two Sum II - Input Array Is Sorted
      • Contains Duplicate
      • Maximum Subarray
      • House Robber
      • Move Zeroes
      • Rotate Array
      • Plus One
      • Find number of subarrays with even length
      • Find number of subarrays with even sum
      • Find Missing Element
      • Reduce Array Size to The Half
      • Remove Duplicates
      • Merge Sorted Arrays
      • Arrays Intersection
      • 3Sum
      • Trapping Rain Water
      • Maximum sum of a subarray
      • Longest Subarray
      • Subarray Sum Equals K
      • Container With Most Water
      • Missing Number
      • Roman to Integer
      • First Missing Positive
      • Unique Integers That Sum Up To 0
      • Integer to Roman
      • Flatten
    • String
      • Check if two strings are permutation of each other
      • String Compression
      • Palindrome Permutation
      • Determine if a string has all Unique Characters
      • One Away
      • Longest Substring Without Repeating Characters
      • Valid Palindrome
      • Valid Palindrome II
      • Backspace String Compare
      • First Unique Character in a String
      • Is Subsequence
      • URLify a given string
      • String has same characters at same position
      • Number of Ways to Split a String
      • Check whether two Strings are anagram of each other
      • Print last `n` lines of a big file or big string.
      • Multiply Strings
    • Matrix
      • Search a 2D Matrix
      • Search a 2D Matrix II
      • Rotate Matrix
      • Spiral Matrix
      • Set Matrix Zeroes
    • Bit Manipulation
      • Sum of Two Integers
      • Reverse Number
      • Reverse Number II
      • Binary Bits Count
      • Binary Bits Count II
    • Stack
      • Valid Parentheses
      • Balance or not expression
      • Decode String
    • Tree
      • Binary Tree Inorder Traversal
      • Binary Tree Preorder Traversal
      • Binary Tree Postorder Traversal
      • Binary Tree Level Order Traversal
      • Binary Tree Return All Root-To-Leaf Paths
      • Binary Tree Height-Balanced
      • Valid Binary Search Tree
      • Binary Tree Sum of all left leaves.
    • Linked List
      • Linked List Delete Middle Node
      • Merge Sorted Linked List
      • Reverse Linked List
      • Remove Duplicates from Sorted List
      • Remove Duplicates from Unsorted List
      • Linked List Cycle
    • Graph
      • The Number Of Islands
      • Number of Closed Islands
      • Max Area of Island
      • Rotting Oranges
      • Number of Provinces
      • Course Schedule
      • Surrounded Regions
      • Snakes and Ladders
      • Widest Path Without Trees
      • Knight Probability in Chessboard
      • Possible moves of knight
      • Check Knight Tour Configuration
      • Steps by Knight
      • Network Delay Time
    • Greedy
      • Best Time to Buy and Sell Stock
      • Best Time to Buy and Sell Stock II
      • Smallest Subset Array
      • Jump Game
    • Backtracking
      • Towers of Hanoi
      • Subsets
      • Combination Sum
      • Sudoku Solver
      • Word Search
    • Heap
      • Kth Largest Element in an Array
      • Top K Frequent Elements
    • Sorting
      • Order Colors String
    • Recursion
      • Number To Text
      • Divide Number
Powered by GitBook
On this page
  1. Problems
  2. Graph

Surrounded Regions

PreviousCourse ScheduleNextSnakes and Ladders

Last updated 1 year ago

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'.

Solutions

Depth-First Search (DFS) Solution: The DFS solution works by treating the board as a graph, where each cell is a node and each node is connected to its neighbors. The algorithm starts by marking all 'O’s on the border of the board as 'B’s, indicating that they are not surrounded by 'X’s and should not be flipped. It does this by performing a DFS starting from each ‘O’ on the border, and for each ‘O’ it encounters during the DFS, it marks it as ‘B’ and continues the DFS on its neighbors.

After all 'O’s connected to the border have been marked as 'B’s, the algorithm then traverses the entire board and flips all remaining 'O’s to 'X’s, because these 'O’s are not connected to the border and are therefore surrounded by 'X’s. It also flips all 'B’s back to 'O’s, because these 'O’s are not surrounded by 'X’s and should not be flipped.

Breadth-First Search (BFS) Solution: The BFS solution works in a similar way, but instead of using DFS to mark the 'O’s that should not be flipped, it uses BFS. The algorithm starts by adding all 'O’s on the border to a queue. Then, while there are still nodes in the queue, it dequeues a node, marks it as ‘B’, and enqueues all of its neighbors that are 'O’s. This process continues until the queue is empty, at which point all 'O’s connected to the border have been marked as 'B’s.

After that, the algorithm traverses the entire board and flips all remaining 'O’s to 'X’s, and flips all 'B’s back to 'O’s, just like in the DFS solution.

Both of these solutions effectively solve the problem by identifying all 'O’s that are not surrounded by 'X’s and marking them so they are not flipped in the final step. The main difference between them is the order in which they visit the nodes (DFS goes deep into the graph first, while BFS visits all neighbors first), but they both produce the correct result.

public class Solution {
    public void Solve(char[][] board) {
        if (board == null || board.Length == 0) return;
        int m = board.Length, n = board[0].Length;
        Queue<int[]> queue = new Queue<int[]>();

        // Enqueue all 'O's on the border
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if ((i == 0 || j == 0 || i == m - 1 || j == n - 1) && board[i][j] == 'O')
                    queue.Enqueue(new int[] { i, j });

        // BFS from the border 'O's
        while (queue.Count > 0) {
            int[] cell = queue.Dequeue();
            int row = cell[0], col = cell[1];
            if (row < 0 || col < 0 || row >= m || col >= n || board[row][col] != 'O') continue;
            board[row][col] = 'B';
            queue.Enqueue(new int[] { row - 1, col });
            queue.Enqueue(new int[] { row + 1, col });
            queue.Enqueue(new int[] { row, col - 1 });
            queue.Enqueue(new int[] { row, col + 1 });
        }

        // Flip all 'O's to 'X's and 'B's back to 'O's
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                board[i][j] = board[i][j] == 'B' ? 'O' : 'X';
    }
}

The time complexity is still O(m*n) and the space complexity is O(min(m, n)) because in the worst case the queue will store all the cells in the border, which is min(m, n).