Transform to Chessboard in C++


Suppose we have one N x N board contains only 0s and 1s. Now in each move, we can swap any 2 rows, or any 2 columns. We have to find the minimum number of moves to transform the board into a "chessboard". If the solution does not exist, then return -1.

So if the input is like −

















Then the output will be 2, as first two columns in the first move, then board will be like −

















Then swap second and 3rd rows −

















This is the chessboard

To solve this, we will follow these steps −

  • n := size of b
  • for initialize i := 0, when i < n, update (increase i by 1), do −
    • for initialize j := 0, when j < n, update (increase j by 1), do −
      • if b[0, 0] XOR b[0, j] XOR b[i, 0] XOR b[i, j] is non-zero, then −
        • return -1
  • rowSum := 0, colSum := 0, rowSwap := 0, colSwap := 0
  • for initialize i := 0, when i < n, update (increase i by 1), do −
    • rowSum := rowSum + b[i, 0], colSum := colSum + b[0, i]
    • rowSwap := true when rowSwap + b[i, 0] is same as i mod 2,
    • colSwap := true when colSwap + b[0, i] is same as i mod 2
  • if rowSum is not equal to n / 2 and rowSum is not equal to (n + 1) / 2, then −
    • return -1
  • if colSum is not equal to n / 2 and colSum is not equal to (n + 1) / 2, then −
    • return -1
  • if n mod 2 is same as 1, then −
    • if colSwap mod 2 is non-zero, then −
      • colSwap := n - colSwap
    • if rowSwap mod 2 is non-zero, then −
      • rowSwap := n - rowSwap
  • Otherwise
    • colSwap := minimum of colSwap and n - colSwap
    • rowSwap := minimum of rowSwap and n - rowSwap
  • return (rowSwap + colSwap) / 2

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int movesToChessboard(vector<vector<int>>& b) {
      int n = b.size();
      for(int i = 0; i < n; i++){
         for(int j = 0; j < n; j++){
            if(b[0][0] ^ b[0][j] ^ b[i][0] ^ b[i][j]) return -1;
         }
      }
      int rowSum = 0;
      int colSum = 0;
      int rowSwap = 0;
      int colSwap = 0;
      for(int i = 0; i < n; i++){
         rowSum += b[i][0];
         colSum += b[0][i];
         rowSwap += b[i][0] == i % 2;
         colSwap += b[0][i] == i % 2;
      }
      if(rowSum != n/2 && rowSum != (n + 1)/2)return -1;
      if(colSum != n/2 && colSum != (n + 1)/2)return -1;
      if(n % 2 == 1){
         if(colSwap % 2) colSwap = n - colSwap;
         if(rowSwap % 2) rowSwap = n - rowSwap;
      }else{
         colSwap = min(colSwap, n - colSwap);
         rowSwap = min(rowSwap, n - rowSwap);
      }
      return (rowSwap + colSwap)/2;
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{0,1,1,0},{0,1,1,0},{1,0,0,1},{1,0,0,1}};
   cout << (ob.movesToChessboard(v));
}

Input

{{0,1,1,0},{0,1,1,0},{1,0,0,1},{1,0,0,1}};

Output

2

Updated on: 02-Jun-2020

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements