Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Capturing Grid
MEDIUM
20 mins
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
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
userLevel
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
yellow-spark
0/80
Avg time to solve 20 mins
Success Rate 70 %
Share
0 upvotes

Problem Statement

Given an ‘M’ x ‘N’ matrix ‘GRID’ containing ‘X’ and ‘O’, capture all the regions that are 4 directionally surrounded by ‘X’ and update the original 'GRID'.

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
Reset Code
Full screen
Auto
copy-code
Console