Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K | Function to count the operations required to convert matrix A to B ; Store the sizes of matrix ; Stores count of flips required ; Traverse the iven matrix ; Check if the matrix values are not equal ; Increment the count of moves ; Check if the current square sized exists or not ; Flip all the bits in this square sized submatrix ; Count of operations required ; Driver Code
function minMoves ( a , b , K ) { let n = a . length , m = a [ 0 ] . length ; let cntOperations = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return - 1 ; } for ( let p = 0 ; p <= K - 1 ; p ++ ) { for ( let q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p ] [ j + q ] == ' ' ) { a [ i + p ] [ j + q ] = ' ' ; } else { a [ i + p ] [ j + q ] = ' ' ; } } } } } } return cntOperations ; } let A = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let B = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let K = 3 ; document . write ( minMoves ( A , B , K ) ) ;