Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K
Given two binary matrices A[] and B[] of dimension N * M and a positive integer K, the task is to find the minimum number of flipping of submatrix of size K required in the matrix A[][] to convert it into the matrix B[][]. If it is not possible to convert then print “-1”.
Examples:
Input: A[][] = { { ‘1’, ‘1’, ‘1’ }, { ‘1’, ‘1’, ‘1’ }, { ‘1’, ‘1’, ‘1’ } }, B[][] = { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, K = 3
Output: 1
Explanation:
Following are the operations performed:
Operation 1: Flip the submatrix of size K from indices (0, 0) modifies the matrix A[][] to { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }.
Therefore, the minimum number of flips required is 1.
Input: A[][] = { { ‘1’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, B[][] = { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, K = 3
Output: -1
Approach: The given problem can be solved using the Greedy Approach, the idea is to traverse the given matrices A[][] and B[][] and if the corresponding cell (i, j) has a different value then flip the current submatrix of size K from indices (i, j) and count this operation of flipping. After traversing the given matrix, if there exist any indices such that submatrix of size K can’t be flipped then print “-1” as it is impossible to convert the matrix A[][] to B[][]. Otherwise, print the count of operations obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minMoves(vector<vector< char >> a,
vector<vector< char >> b,
int K)
{
int n = a.size(), m = a[0].size();
int cntOperations = 0;
for ( int i = 0; i < n; i++) {
for ( int 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 ( int p = 0;
p <= K - 1; p++) {
for ( int q = 0;
q <= K - 1; q++) {
if (a[i + p][j + q] == '0' ) {
a[i + p][j + q] = '1' ;
}
else {
a[i + p][j + q] = '0' ;
}
}
}
}
}
}
return cntOperations;
}
int main()
{
vector<vector< char > > A = { { '1' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
vector<vector< char > > B = { { '0' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
int K = 3;
cout << minMoves(A, B, K);
return 0;
}
|
Java
public class GFG {
static int minMoves( char a[][], char b[][], int K)
{
int n = a.length;
int m = a[ 0 ].length;
int cntOperations = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int 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 ( int p = 0 ;
p <= K - 1 ; p++) {
for ( int q = 0 ;
q <= K - 1 ; q++) {
if (a[i + p][j + q] == '0' ) {
a[i + p][j + q] = '1' ;
}
else {
a[i + p][j + q] = '0' ;
}
}
}
}
}
}
return cntOperations;
}
public static void main (String[] args)
{
char A[][] = { { '1' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
char B[][] = { { '0' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
int K = 3 ;
System.out.println(minMoves(A, B, K));
}
}
|
Python3
def minMoves(a, b, K):
n = len (a)
m = len (a[ 0 ])
cntOperations = 0
for i in range ( 0 , n):
for j in range ( 0 , m):
if (a[i][j] ! = b[i][j]):
cntOperations + = 1
if (i + K - 1 > = n or j + K - 1 > = m):
return - 1
for p in range ( 0 , K):
for q in range ( 0 , K):
if (a[i + p][j + q] = = '0' ):
a[i + p][j + q] = '1'
else :
a[i + p][j + q] = '0'
return cntOperations
if __name__ = = "__main__" :
A = [[ '1' , '0' , '0' ], [ '0' , '0' , '0' ], [ '0' , '0' , '0' ]]
B = [[ '0' , '0' , '0' ], [ '0' , '0' , '0' ], [ '0' , '0' , '0' ]]
K = 3
print (minMoves(A, B, K))
|
C#
using System;
class GFG
{
static int minMoves( char [,] a, char [,] b, int K)
{
int n = a.Length;
int m = a.GetLength(0);
int cntOperations = 0;
for ( int i = 0; i < n; i++)
{
for ( int 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 ( int p = 0;
p <= K - 1; p++)
{
for ( int q = 0;
q <= K - 1; q++)
{
if (a[i + p, j + q] == '0' )
{
a[i + p, j + q] = '1' ;
}
else
{
a[i + p, j + q] = '0' ;
}
}
}
}
}
}
return cntOperations;
}
public static void Main()
{
char [,] A = new char [,]{ { '1' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
char [,] B = new char [,]{ { '0' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
int K = 3;
Console.WriteLine(minMoves(A, B, K));
}
}
|
Javascript
<script>
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] == '0' ) {
a[i + p][j + q] = '1' ;
}
else {
a[i + p][j + q] = '0' ;
}
}
}
}
}
}
return cntOperations;
}
let A = [[ '1' , '0' , '0' ],
[ '0' , '0' , '0' ],
[ '0' , '0' , '0' ]];
let B = [[ '0' , '0' , '0' ],
[ '0' , '0' , '0' ],
[ '0' , '0' , '0' ]];
let K = 3;
document.write(minMoves(A, B, K));
</script>
|
Time Complexity: O(N*M*K2)
Auxiliary Space: O(1)
Last Updated :
16 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...