653. Capture the Regions
A landscaper sketches a garden on a rectangular board. A cell marked 'O' is open pond and a cell marked 'X' is stone. Open cells that touch along a side (up, down, left or right) belong to the same pond. A pond is sheltered when none of its cells lies in the first row, the last row, the first column or the last column of the board.
Modify board in place so that every cell of every sheltered pond becomes 'X'. Ponds that reach the edge of the board, and all stone cells, stay exactly as they are. The method returns nothing. The intended solution runs in O(R * C) time, uses at most O(R * C) extra space and avoids deep recursion.
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:
The pond made of the three central cells is enclosed by stone and gets filled; the pond cell in the bottom row touches the edge and stays.
Example 2
- Input:
- board = [["O","X","O"],["X","O","X"],["O","X","O"]]
- Output:
- [["O","X","O"],["X","X","X"],["O","X","O"]]
- Explanation:
The middle pond cell is enclosed on all four sides by stone, so it is filled. The corner ponds touch the edge.
Example 3
- Input:
- board = [["O","O"],["O","O"]]
- Output:
- [["O","O"],["O","O"]]
- Explanation:
Every pond cell lies on the edge, so nothing changes.
Constraints
1 ≤ R, C ≤ 400
board[i][j] is 'X' or 'O'
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Graded
- Your answer is read from
boardafter your method returns. - Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(R * C)
- Space
- O(R * C)