[LeetCode] Transform to Chessboard 转为棋盘

 

An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or any 2 columns with each other.html

What is the minimum number of moves to transform the board into a "chessboard" - a board where no 0s and no 1s are 4-directionally adjacent? If the task is impossible, return -1.数组

Examples:
Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
Output: 2
Explanation:
One potential sequence of moves is shown below, from left to right:

0110     1010     1010
0110 --> 1010 --> 0101
1001     0101     1010
1001     0101     0101

The first move swaps the first and second column.
The second move swaps the second and third row.


Input: board = [[0, 1], [1, 0]]
Output: 0
Explanation:
Also note that the board with 0 in the top left corner,
01
10

is also a valid chessboard.

Input: board = [[1, 0], [1, 0]]
Output: -1
Explanation:
No matter what sequence of moves you make, you cannot end with a valid chessboard.

Note:优化

  • board will have the same number of rows and columns, a number in the range [2, 30].
  • board[i][j] will be only 0s or 1s.

 

这道题给了咱们一个二维数组,里面都是由0和1组成的,让咱们经过交换行或者列来造成一个棋盘。棋盘咱们都见过吧,就是国际象棋的那种棋盘,黑白相间的那种,用数组表示就是0和1交替出现,相邻位置上的数字一定不是同样的。这道题默认的棋盘的起始位置能够是1或者0,而后依次类推可获得全部位置上的值。这道题最大的难点是在于判断给定的数组最终可否组成棋盘,由于能经过交换组成棋盘的数组实际上是有不少苛刻条件须要知足的,只有这些条件都知足了,才能到计算交换数到那一步。首先咱们先来看长度为4的棋盘:spa

1 0 1 0code

0 1 0 1orm

1 0 1 0htm

0 1 0 1blog

或者:ci

0 1 0 1leetcode

1 0 1 0

0 1 0 1

1 0 1 0

咱们发现对于长度为偶数的棋盘,每一行0和1的个数都是相等的,无论咱们如何交换行和列,0和1的个数都是不会变化的,再看看长度为奇数的棋盘,好比3:

1 0 1

0 1 0

1 0 1

或者:

0 1 0

1 0 1

0 1 0

咱们发现对于长度为奇数的棋盘,各行的0和1个数不一样,可是仍是有规律的,每行的1的个数要么为 n/2,要么为 (n+1)/2,这个规律必定要保证,否则没法造成棋盘。

还有一个很重要的规律,咱们观察题目给的第一个例子,若是咱们只看行,咱们发现只有两种状况 0110 和 1001,若是只看列,只有 0011 和 1100,咱们发现无论棋盘有多长,都只有两种状况,而这两种状况上各位上是相反的,只有这样的矩阵才有可能转换为棋盘。那么这个规律能够衍生出一个规律,就是任意一个矩形的四个顶点只有三种状况,要么四个0,要么四个1,要么两个0两个1,不会有其余的状况。那么四个顶点亦或在一块儿必定是0,因此咱们判断只要亦或出了1,必定是不对的,直接返回-1。以后咱们来统计首行和首列中的1个数,由于咱们要让其知足以前提到的规律。统计完了首行首列1的个数,咱们判断若是其小于 n/2 或者大于 (n+1) / 2,那么必定没法转为棋盘。咱们还须要算下首行和首列跟棋盘位置的错位的个数,虽然 01010 和 10101 均可以是正确的棋盘,咱们先默认跟 10101 比较好了,以后再作优化处理。

最后的难点就是计算最小的交换步数了,这里要分n的奇偶来讨论。若是n是奇数,咱们必须获得偶数个,为啥呢,由于咱们以前统计的是跟棋盘位置的错位的个数,而每次交换行或者列,会修改两个错位,因此若是是奇数就没法还原为棋盘。举个例子,好比首行是 10001,若是咱们跟棋盘 10101 比较,只有一个错位,可是咱们是没法经过交换获得 10101的,因此咱们必需要交换获得 01010,此时的错位是4个,而咱们经过 n - rowDiff 正好也能获得4,这就是为啥咱们须要偶数个错位。若是n是偶数,那么就不会出现这种问题,可是会出现另外一个问题,好比咱们是 0101,这自己就是正确的棋盘排列了,可是因为咱们默认是跟 1010 比较,那么咱们会获得4个错位,因此咱们应该跟 n - rowDiff 比较取较小值。列的处理跟行的处理彻底同样。最终咱们把行错位个数跟列错位个数相加,再除以2,就能够获得最小的交换次数了,以前说过了每交换一次,能够修复两个错位,参见代码以下:

 

class Solution {
public:
    int movesToChessboard(vector<vector<int>>& board) {
        int n = board.size(), rowSum = 0, colSum = 0, rowDiff = 0, colDiff = 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]) return -1;
            }
        }
        for (int i = 0; i < n; ++i) {
            rowSum += board[0][i];
            colSum += board[i][0];
            rowDiff += (board[i][0] == i % 2);
            colDiff += (board[0][i] == i % 2);
        }
        if (n / 2 > rowSum || rowSum > (n + 1) / 2) return -1;
        if (n / 2 > colSum || colSum > (n + 1) / 2) return -1;
        if (n % 2) {
            if (rowDiff % 2) rowDiff = n - rowDiff;
            if (colDiff % 2) colDiff = n - colDiff;
        } else {
            rowDiff = min(n - rowDiff, rowDiff);
            colDiff = min(n - colDiff, colDiff);
        }
        return (rowDiff + colDiff) / 2;
    }
};

 

参考资料:

https://leetcode.com/problems/transform-to-chessboard/

https://leetcode.com/problems/transform-to-chessboard/discuss/132113/Java-Clear-Code-with-Detailed-Explanations

https://leetcode.com/problems/transform-to-chessboard/discuss/114847/Easy-and-Concise-Solution-with-Explanation-C%2B%2BJavaPython

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索