The Wayback Machine - https://web.archive.org/web/20211015204222/https://www.geeksforgeeks.org/minimum-operations-to-convert-binary-matrix-a-to-b-by-flipping-submatrix-of-size-k/
Skip to content
Related Articles

Related Articles

Improve Article

Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K

  • Last Updated : 15 Oct, 2021

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: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the operations
// required  to convert matrix A to B
int minMoves(vector<vector<char>> a,
             vector<vector<char>> b,
             int K)
{
    // Store the sizes of matrix
    int n = a.size(), m = a[0].size();
  
      // Stores count of flips required
    int cntOperations = 0;
  
    // Traverse the iven matrix
    for (int i = 0; i < n; i++) {
  
        for (int j = 0; j < m; j++) {
  
            // Check if the matrix values
              // are not equal
            if (a[i][j] != b[i][j]) {
  
                // Increment the count
                  // of moves
                cntOperations++;
  
                // Check if the current
                  // square sized exists
                  // or not
                if (i + K - 1 >= n
                    || j + K - 1 >= m) {
                    return -1;
                }
  
                // Flip all the bits in this
                // square sized submatrix
                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';
                        }
                    }
                }
            }
        }
    }
  
    // Count of operations required
    return cntOperations;
}
  
// Driver Code
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;
}
Output
-1

Time Complexity: O(N*M*K2)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :