782-Transform to Chessboard

Description:
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.

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:

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

题目描述;
给定一个二维数组,其元素为0或1,你可以在每一步中交换两行或者两列。问将这个二维数组转换为一个合法的’棋盘’(0和1不会在4个方向相邻)的最小步数是多少?若无法转换,则返回-1

思路;
首先,要明白满足什么条件是一个合法的棋盘.条件如下:
1.该棋盘的行只有两种。如果一种为0110,那么另一种必为1001(即每位相异)。列同理
2.该棋盘的行或列的零的个数当N为偶数时为 N2 N 2 ,当N为奇数时为 N2N+12 N 2 或 N + 1 2

然后是转换,我们的目标是将其转换为形如0101…或者1010…分为两种情况:
1.当N为偶数时,返回最小的那个就可以了
2.当N为奇数时,返回偶数次交换的那个

解法:

class Solution {
    public int movesToChessboard(int[][] b) {
        int N = b.length, rowSum = 0, colSum = 0, rowSwap = 0, colSwap = 0;
        for (int i = 0; i < N; ++i){
            for (int j = 0; j < N; ++j){
                if ((b[0][0] ^ b[i][0] ^ b[0][j] ^ b[i][j]) == 1) return -1;
            }
        }
        for (int i = 0; i < N; ++i) {
            rowSum += b[0][i];
            colSum += b[i][0];
            if (b[i][0] == i % 2) rowSwap ++;
            if (b[0][i] == i % 2) colSwap ++ ;
        }
        if (N / 2 > rowSum || rowSum > (N + 1) / 2) return -1;
        if (N / 2 > colSum || colSum > (N + 1) / 2) return -1;
        if (N % 2 == 1) {
            if (colSwap % 2 == 1) colSwap = N - colSwap;
            if (rowSwap % 2 == 1) rowSwap = N - rowSwap;
        } else {
            colSwap = Math.min(N - colSwap, colSwap);
            rowSwap = Math.min(N - rowSwap, rowSwap);
        }
        return (colSwap + rowSwap) / 2;
    }
}

你可能感兴趣的:(算法与数据结构,leetcode全解)