782. Transform to Chessboard Hard

@problem@discussion
#Array#Math#Bit Manipulation#Matrix



1/**
2 * [782] Transform to Chessboard
3 *
4 * 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.
5 * Return the minimum number of moves to transform the board into a chessboard board. If the task is impossible, return -1.
6 * A chessboard board is a board where no 0's and no 1's are 4-directionally adjacent.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/chessboard1-grid.jpg" style="width: 500px; height: 145px;" />
10 * Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
11 * Output: 2
12 * Explanation: One potential sequence of moves is shown.
13 * The first move swaps the first and second column.
14 * The second move swaps the second and third row.
15 * 
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/chessboard2-grid.jpg" style="width: 164px; height: 165px;" />
18 * Input: board = [[0,1],[1,0]]
19 * Output: 0
20 * Explanation: Also note that the board with 0 in the top left corner, is also a valid chessboard.
21 * 
22 * Example 3:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/chessboard3-grid.jpg" style="width: 164px; height: 165px;" />
24 * Input: board = [[1,0],[1,0]]
25 * Output: -1
26 * Explanation: No matter what sequence of moves you make, you cannot end with a valid chessboard.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	n == board.length
32 * 	n == board[i].length
33 * 	2 <= n <= 30
34 * 	board[i][j] is either 0 or 1.
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/transform-to-chessboard/
40// discuss: https://leetcode.com/problems/transform-to-chessboard/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn moves_to_chessboard(board: Vec<Vec<i32>>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_782() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.