Bitmap Holes
Have the function BitmapHoles (strarr) take the
array of strings stored in strArr, which will be a 2D
matrix of 0 and 1's, and determine how many holes,
or contiguous regions of O's, exist in the matrix. A
contiguous region is one where there is a connected
group of O's going in one or more of four directions:
up, down, left, or right. For example: if strArris
["10111", "10101","11101","11111"), then this looks
like the following matrix:
1011 1
1 0 1 0 1
1 1 1 0 1
1 1 1 1 1
Answers
10101
Explanation:
BitmapHoles(strArr) take the array of strings stored in a string, which will be a 2D matrix. of 0 and 1's, and determine how many holes, or contiguous regions of 0's, exist in the matrix.
function BitmapHoles(strArr)
{
let bitmap = strArr.map(a => a.split(''));
console.log(bitmap)
let count = 2;
for(let i = 0; i < bitmap.length; i++)
{
for(let j = 0; j < bitmap[i].length; j++)
{
if(bitmap[i][j] === '0') {
coverHole(bitmap, i, j, count++);
}
}
}
return count - 2;
}
function coverHole(bitmap, i, j, number){
bitmap[i][j] = number;
if (+bitmap[i][j-1] === 0) {
bitmap[i][j-1] = number;
coverHole(bitmap, i, j-1, number);
}
if (+bitmap[i][j+1] === 0) {
bitmap[i][j+1]= number;
coverHole(bitmap, i, j+1, number);
}
if(bitmap[i-1] !== undefined && +bitmap[i-1][j] === 0) {
bitmap[i-1][j]= number;
coverHole(bitmap, i-1, j, number);
}
if(bitmap[i+1] !== undefined && +bitmap[i+1][j] === 0) {
bitmap[i+1][j]= number;
coverHole(bitmap, i+1, j, number);
}
return;
}
BitmapHoles(["01111", "01101", "00011", "11110"])