Problem: Transform to Chessboard

You are given an `n x n` binary grid `board`. In each move, you can swap any
two rows with each other, or any two columns with each other.

Return the minimum number of moves to transform the board into a chessboard
board. If the task is impossible, return `-1`.

A chessboard board is a board where no `0`'s and no `1`'s are 4-directionally
adjacent.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def movesToChessboard(self, board: List[List[int]]) -> int:
    n = len(board)

    for i in range(n):
      for j in range(n):
        if board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]:
          return -1

    rowSum = sum(board[0])
    colSum = sum(board[i][0] for i in range(n))

    if rowSum != n // 2 and rowSum != (n + 1) // 2:
      return -1
    if colSum != n // 2 and colSum != (n + 1) // 2:
      return -1

    rowSwaps = sum(board[i][0] == (i & 1) for i in range(n))
    colSwaps = sum(board[0][i] == (i & 1) for i in range(n))

    if n & 1:
      if rowSwaps & 1:
        rowSwaps = n - rowSwaps
      if colSwaps & 1:
        colSwaps = n - colSwaps
    else:
      rowSwaps = min(rowSwaps, n - rowSwaps)
      colSwaps = min(colSwaps, n - colSwaps)

    return (rowSwaps + colSwaps) // 2


Java solution:
class Solution {
  public int movesToChessboard(int[][] board) {
    final int n = board.length;
    int rowSum = 0;
    int colSum = 0;
    int rowSwaps = 0;
    int colSwaps = 0;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if ((board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) == 1)
          return -1;

    for (int i = 0; i < n; ++i) {
      rowSum += board[0][i];
      colSum += board[i][0];
    }

    if (rowSum != n / 2 && rowSum != (n + 1) / 2)
      return -1;
    if (colSum != n / 2 && colSum != (n + 1) / 2)
      return -1;

    for (int i = 0; i < n; ++i) {
      if (board[i][0] == (i & 1))
        ++rowSwaps;
      if (board[0][i] == (i & 1))
        ++colSwaps;
    }

    if (n % 2 == 1) {
      if (rowSwaps % 2 == 1)
        rowSwaps = n - rowSwaps;
      if (colSwaps % 2 == 1)
        colSwaps = n - colSwaps;
    } else {
      rowSwaps = Math.min(rowSwaps, n - rowSwaps);
      colSwaps = Math.min(colSwaps, n - colSwaps);
    }

    return (rowSwaps + colSwaps) / 2;
  }
}


C++ solution:
class Solution {
 public:
  int movesToChessboard(vector<vector<int>>& board) {
    const int n = board.size();
    int rowSum = 0;
    int colSum = 0;
    int rowSwaps = 0;
    int colSwaps = 0;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j] == 1)
          return -1;

    for (int i = 0; i < n; ++i) {
      rowSum += board[0][i];
      colSum += board[i][0];
    }

    if (rowSum != n / 2 && rowSum != (n + 1) / 2)
      return -1;
    if (colSum != n / 2 && colSum != (n + 1) / 2)
      return -1;

    for (int i = 0; i < n; ++i) {
      rowSwaps += board[i][0] == (i & 1);
      colSwaps += board[0][i] == (i & 1);
    }

    if (n % 2 == 1) {
      if (rowSwaps % 2 == 1)
        rowSwaps = n - rowSwaps;
      if (colSwaps % 2 == 1)
        colSwaps = n - colSwaps;
    } else {
      rowSwaps = min(rowSwaps, n - rowSwaps);
      colSwaps = min(colSwaps, n - colSwaps);
    }

    return (rowSwaps + colSwaps) / 2;
  }
};