Check if there are T number of continuous of blocks of 0 s or not in given Binary Matrix | Javascript program for the above approach ; Stores the moves in the matrix ; Function to find if the current cell lies in the matrix or not ; Function to perform the DFS Traversal ; Iterate over the direction vector ; DFS Call ; Function to check if it satisfy the given criteria or not ; Keeps the count of cell having value as 0 ; If condition doesn 't satisfy ; Keeps the track of unvisted cell having values 0 ; Increasing count of black_spot ; Find the GCD of N and M ; Prlet the result ; Driver Code
let M = 1e9 + 7 ; let directions = [ [ 0 , 1 ] , [ - 1 , 0 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ 1 , 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , - 1 ] , ] ; function isInside ( i , j , N , M ) { if ( i >= 0 && i < N && j >= 0 && j < M ) { return true ; } return false ; } function DFS ( mat , N , M , i , j , visited ) { if ( visited [ i ] [ j ] == true ) { return ; } visited [ i ] [ j ] = true ; for ( let it of directions ) { let I = i + it [ 0 ] ; let J = j + it [ 1 ] ; if ( isInside ( I , J , N , M ) ) { if ( mat [ I ] [ J ] == 0 ) { DFS ( mat , N , M , I , J , visited ) ; } } } } function check ( N , M , mat ) { let black = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) { black ++ ; } } } if ( black < 2 * Math . max ( N , M ) ) { document . write ( " " ) ; return ; } let visited = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < M ; j ++ ) { temp . push ( false ) ; } visited . push ( temp ) ; } let black_spots = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( visited [ i ] [ j ] == false && mat [ i ] [ j ] == 0 ) { black_spots ++ ; DFS ( mat , N , M , i , j , visited ) ; } } } let T = __gcd ( N , M ) ; cout << ( black_spots >= T ? " " : " " ) ; } let N = 3 ; M = 3 ; let mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 0 , 0 , 1 ] , ] ; check ( M , N , mat ) ;