Surrounded Regions
LeetCode Problem #130 (Medium)
Problem Statement
Given a 2D board containing 'X'
and 'O'
(the letter O), capture all regions surrounded by 'X'
. A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
Surrounded regions shouldn’t be on the border, which means that any 'O'
on the border of the board are not flipped to 'X'
. Any 'O'
that is not on the border and it is not connected to an 'O'
on the border will be flipped to 'X'
. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Testcases
Problem Explanation
In this problem, we must analyse the condition which allows a cell to be converted from 'O'
to 'X'
.
According to the problem, if a cell on the border (row or column) is an 'O'
then it cannot be converted to 'X'
. The problem also stipulates that if a cell with an 'O'
is connected to a border cell with an 'O'
then it cannot be converted to 'X'
either.
Example:
In the first example, none of the cells with 'O'
can be converted to 'X'
. This is because the cell at position [1][2]
contains an 'O'
and is on the border of the board. The other cells which contain an 'O'
are connected to this cell, so according to the given constraints, these cells cannot be converted to 'X'
. Therefore, if a chain of adjacent 'O's
are connected to an 'O'
on a boundary, then it cannot be converted.
In this example, the cells containing an 'O'
are not located on the border of the board so the cells can be converted to 'X'
.
Prerequisite Knowledge
Depth-First-Search
The Depth-First-Search (DFS) algorithm is used to traverse or search for elements in a tree or graph. The traversal takes place in a depth-wise manner such that we start at a particular node and travel as far as possible down the particular branch associated with that node before backtracking and traversing through the remaining branches.
The basic idea behind DFS is to start from the root node or any arbitrary node and mark the node as visited. Then, we move to the adjacent unmarked node and continue this process until there is no unmarked adjacent node remaining. We then backtrack and check for other unmarked nodes and traverse through them. Finally, the nodes that have been traversed in the path will be printed.
Algorithm
Depth-First-Search Approach
Iterate over the row and column boundaries of the board until an 'O' is encountered.
Perform depth-first-search using the DFS helper function every time an 'O' is encountered, from its position.
Convert all of the encountered 'O's during the DFS operation to '#' in order to mark the cells that cannot be converted to 'X'.
When the DFS has been completed, there are three elements remaining on the board:
a. 'X' - Do not make any changes.
b. 'O' - Convert to 'X', since they are not connected to any boundary and therefore can be converted to 'X'.
c. '#' - Convert back to 'O' since they are connected to some boundary and therefore cannot be converted to 'X'.
Code
Last updated
Was this helpful?