Close
Topic list
Capturing Grid
MEDIUM
20 mins
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Capturing Grid

Contributed by
imObscure
Medium
0/80
Avg time to solve 20 mins
Success Rate 70 %
Share

## Problem Statement

#### A region is captured by flipping all ‘O’s to ‘X’s in that surrounding region.

##### Example:
``````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.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
3 3
XXX
XOX
XXX
3 4
XXXO
XXOX
XOXX
``````
##### Sample Output 1 :
``````XXX
XXX
XXX
XXXO
XXXX
XOXX
``````
##### Explanation Of Sample Input 1 :
``````Test 1:
The ‘O’ is surrounded by ‘X’s in all directions, so it is captured and flipped.

Test 2:
The ‘O’ in the first row is only surrounded by 2 sides.
The ‘O’ in the second row is surrounded by all 4 sides, hence
flipped.
The ‘O’ in the third row is only surrounded by 3 sides.
``````
##### Sample Input 2 :
``````2
3 3
XXX
XOX
XOX
3 4
OOXO
OXOX
OOXO
``````
##### Sample Output 2 :
``````XXX
XOX
XOX
OOXO
OXXX
OOXO
``````
Auto
Console