Illuminate Surrounded Regions
MATRIX
Problem
Given a 2D binary matrix matrix
, transform all '0's that are completely surrounded by '1's into '1's. Any '0's that are on the edge of the matrix or are connected to an edge '0' (directly or through other '0's) should remain as '0's.
Examples
illuminateRegions([ [1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1], [1, 1, 1, 1] ]) // returns [ [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], ] // All '0's are surrounded by '1's and are transformed into '1's illuminateRegions([ [1, 1, 1, 1, 1], [1, 1, 0, 0, 1], [1, 1, 1, 0, 1], [1, 1, 0, 0, 1], [1, 0, 1, 1, 1] ]) // returns [ [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 1 ], [ 1, 0, 1, 1, 1 ] ]
Loading...
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1