
Input:
3 3
XXX
XOX
XXX
Output:
XXX
XXX
XXX
Explanation:
The ‘O’ is surrounded by ‘X’s in all directions, so it is captured and flipped.
The first line of the input contains an integer, ‘T’, denoting the number of test cases.
The First line of each test case contains two integers, ‘N’ and ‘M’ denoting the number of rows and columns in the ‘GRID’.
The next ‘N’ lines of each test case contain ‘M’ characters, ‘O’ or ‘X’.
Modify the original ‘GRID’ by flipping the captured ‘O’s.
1 <= T <= 10
1 <= N,M <= 200
Time Limit: 1 sec
The solution to the problem lies in marking all the connected components of ‘O’s which also touches the boundary of the GRID as they are not surrounded by ‘X’s from all 4 directions so they can not be captured.
This can be implemented by performing Depth First Search for every ‘O’ at the boundary of the GRID and marking them. Now we can reiterate through the GRID and change the unmarked ‘O’s to ‘X’s and remaining to ‘O’s .