Count all 0s which are blocked by 1s in binary matrix
Given Binary matrix. The task is to count all zeros which are surrounded by one (may not be an immediate neighbor).
Note: here we are only taking four directions up, left, down, right.
Examples:
Input : Int M[][] = {{ 0, 1, 1, 0},
{ 1, 0, 0, 1},
{ 0, 1, 0, 1},
{ 1, 0, 1, 1}}
Output : 3
Explanation : All zeros which are surrounded
by 1 are (1, 1), (1, 2) and (2, 2)
Idea is based on the DFS.
First we remove all the zero value cells in the matrix which are reachable from the boundary of Matrix using DFS. Note that any cell which is reachable from a boundary 0 cell is not surrounded by ones.
Int M[][] = {{ 0, 1, 1, 0},
{ 1, 0, 0, 1},
{ 0, 1, 1, 1},
{ 1, 0, 1, 1}}
All zero cell which are reachable from boundary are in red color.
After that, we count all zeros which are left in binary matrix.
Below is the implementation of above idea.
C++
#include <iostream>
using namespace std;
#define Row 4
#define Col 5
int r[4] = { 0, 0, 1, -1 };
int c[4] = { 1, -1, 0, 0 };
bool isSafe( int x, int y, int M[][Col])
{
if (x >= 0 && x <= Row && y >= 0 &&
y <= Col && M[x][y] == 0)
return true ;
return false ;
}
void DFS( int x, int y, int M[][Col])
{
M[x][y] = 1;
for ( int k = 0; k < 4; k++)
if (isSafe(x + r[k], y + c[k], M))
DFS(x + r[k], y + c[k], M);
}
int CountAllZero( int M[][Col])
{
for ( int i = 0; i < Col; i++)
if (M[0][i] == 0)
DFS(0, i, M);
for ( int i = 0; i < Col; i++)
if (M[Row - 1][i] == 0)
DFS(Row - 1, i, M);
for ( int i = 0; i < Row; i++)
if (M[i][0] == 0)
DFS(i, 0, M);
for ( int i = 0; i < Row; i++)
if (M[i][Col - 1] == 0)
DFS(i, Col - 1, M);
int result = 0;
for ( int i = 0; i < Row; i++)
for ( int j = 0; j < Col; j++)
if (M[i][j] == 0)
result++;
return result;
}
int main()
{
int M[][Col] = { { 1, 1, 1, 0, 1 },
{ 1, 0, 0, 1, 0 },
{ 1, 0, 1, 0, 1 },
{ 0, 1, 1, 1, 1 } };
cout << CountAllZero(M) << endl;
return 0;
}
|
Java
class GFG {
final static int Row = 4 ;
final static int Col = 5 ;
static int r[] = { 0 , 0 , 1 , - 1 };
static int c[] = { 1 , - 1 , 0 , 0 };
static boolean isSafe( int x, int y, int M[][]) {
if (x >= 0 && x <Row && y >= 0
&& y < Col && M[x][y] == 0 ) {
return true ;
}
return false ;
}
static void DFS( int x, int y, int M[][]) {
M[x][y] = 1 ;
for ( int k = 0 ; k < 4 ; k++) {
if (isSafe(x + r[k], y + c[k], M)) {
DFS(x + r[k], y + c[k], M);
}
}
}
static int CountAllZero( int M[][]) {
for ( int i = 0 ; i < Col; i++) {
if (M[ 0 ][i] == 0 ) {
DFS( 0 , i, M);
}
}
for ( int i = 0 ; i < Col; i++) {
if (M[Row - 1 ][i] == 0 ) {
DFS(Row - 1 , i, M);
}
}
for ( int i = 0 ; i < Row; i++) {
if (M[i][ 0 ] == 0 ) {
DFS(i, 0 , M);
}
}
for ( int i = 0 ; i < Row; i++) {
if (M[i][Col - 1 ] == 0 ) {
DFS(i, Col - 1 , M);
}
}
int result = 0 ;
for ( int i = 0 ; i < Row; i++) {
for ( int j = 0 ; j < Col; j++) {
if (M[i][j] == 0 ) {
result++;
}
}
}
return result;
}
public static void main(String[] args) {
int M[][] = {{ 1 , 1 , 1 , 0 , 1 },
{ 1 , 0 , 0 , 1 , 0 },
{ 1 , 0 , 1 , 0 , 1 },
{ 0 , 1 , 1 , 1 , 1 }};
System.out.print(CountAllZero(M));
}
}
|
Python3
Row = 4
Col = 5
r = [ 0 , 0 , 1 , - 1 ]
c = [ 1 , - 1 , 0 , 0 ]
def isSafe(x, y, M):
if (x > = 0 and x < Row and y > = 0 and y < Col):
if M[x][y] = = 0 :
return True
return False
def DFS(x, y, M):
M[x][y] = 1
for k in range ( 4 ):
if (isSafe(x + r[k], y + c[k], M)):
DFS(x + r[k], y + c[k], M)
def CountAllZero(M):
for i in range (Col):
if (M[ 0 ][i] = = 0 ):
DFS( 0 , i, M)
for i in range (Col):
if (M[Row - 1 ][i] = = 0 ):
DFS(Row - 1 , i, M)
for i in range (Row):
if (M[i][ 0 ] = = 0 ):
DFS(i, 0 , M)
for i in range (Row):
if (M[i][Col - 1 ] = = 0 ):
DFS(i, Col - 1 , M)
result = 0
for i in range (Row):
for j in range (Col):
if (M[i][j] = = 0 ):
result + = 1
return result
M = [[ 1 , 1 , 1 , 0 , 1 ], [ 1 , 0 , 0 , 1 , 0 ],
[ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 1 ]]
print (CountAllZero(M))
|
C#
using System;
public class GFG {
readonly static int Row = 4;
readonly static int Col = 5;
static int []r = {0, 0, 1, -1};
static int []c = {1, -1, 0, 0};
static bool isSafe( int x, int y, int [,]M) {
if (x >= 0 && x <Row && y >= 0
&& y < Col && M[x,y] == 0) {
return true ;
}
return false ;
}
static void DFS( int x, int y, int [,]M) {
M[x,y] = 1;
for ( int k = 0; k < 4; k++) {
if (isSafe(x + r[k], y + c[k], M)) {
DFS(x + r[k], y + c[k], M);
}
}
}
static int CountAllZero( int [,]M) {
for ( int i = 0; i < Col; i++) {
if (M[0,i] == 0) {
DFS(0, i, M);
}
}
for ( int i = 0; i < Col; i++) {
if (M[Row - 1,i] == 0) {
DFS(Row - 1, i, M);
}
}
for ( int i = 0; i < Row; i++) {
if (M[i,0] == 0) {
DFS(i, 0, M);
}
}
for ( int i = 0; i < Row; i++) {
if (M[i,Col - 1] == 0) {
DFS(i, Col - 1, M);
}
}
int result = 0;
for ( int i = 0; i < Row; i++) {
for ( int j = 0; j < Col; j++) {
if (M[i,j] == 0) {
result++;
}
}
}
return result;
}
public static void Main() {
int [,]M = {{1, 1, 1, 0, 1},
{1, 0, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 1, 1, 1}};
Console.Write(CountAllZero(M));
}
}
|
Javascript
<script>
var Row = 4;
var Col = 5;
var r = [ 0, 0, 1, -1 ];
var c = [ 1, -1, 0, 0 ];
function isSafe(x, y, M)
{
if (x >= 0 && x < Row && y >= 0 &&
y < Col && M[x][y] == 0)
return true ;
return false ;
}
function DFS(x, y, M)
{
M[x][y] = 1;
for ( var k = 0; k < 4; k++)
if (isSafe(x + r[k], y + c[k], M))
DFS(x + r[k], y + c[k], M);
}
function CountAllZero(M)
{
for ( var i = 0; i < Col; i++)
if (M[0][i] == 0)
DFS(0, i, M);
for ( var i = 0; i < Col; i++)
if (M[Row - 1][i] == 0)
DFS(Row - 1, i, M);
for ( var i = 0; i < Row; i++)
if (M[i][0] == 0)
DFS(i, 0, M);
for ( var i = 0; i < Row; i++)
if (M[i][Col - 1] == 0)
DFS(i, Col - 1, M);
var result = 0;
for ( var i = 0; i < Row; i++)
for ( var j = 0; j < Col; j++)
if (M[i][j] == 0)
result++;
return result;
}
var M = [ [ 1, 1, 1, 0, 1 ],
[ 1, 0, 0, 1, 0 ],
[ 1, 0, 1, 0, 1 ],
[ 0, 1, 1, 1, 1 ] ];
document.write( CountAllZero(M));
</script>
|
Time Complexity: O(Row*Col)
Auxiliary Space: O(Row×Col)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...