Problem
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.
Example:
1 | |
After running your function, the board should be:
1 | |
Explanation
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.
Explanation
- First, we can find the boarder’s character, if they are ‘O’, then we flood fill them to ‘Y’.
- Next, we can replace the rest of ‘O’ to be ‘X’ since they are not connected to the boarder.
- Finally, replace all ‘Y’ back to ‘O’.
Solution
1 | |