From bc89dfbffe27968ff38c91b939f04d965589b177 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 8 Dec 2024 12:14:03 +0800 Subject: [PATCH] 20241208 finished. --- src/problem/mod.rs | 2 + src/problem/p782_transform_to_chessboard.rs | 100 ++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/problem/p782_transform_to_chessboard.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index afd8daa..f6cca7e 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -356,3 +356,5 @@ mod p3001_minimum_moves_to_capture_the_queen; mod p999_available_captures_for_rook; mod p688_knight_probability_in_chessboard; + +mod p782_transform_to_chessboard; diff --git a/src/problem/p782_transform_to_chessboard.rs b/src/problem/p782_transform_to_chessboard.rs new file mode 100644 index 0000000..13bf6ca --- /dev/null +++ b/src/problem/p782_transform_to_chessboard.rs @@ -0,0 +1,100 @@ +/** + * [782] Transform to Chessboard + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn moves_to_chessboard(board: Vec>) -> i32 { + let n = board.len(); + + let (mut row_mask, mut col_mask) = (0, 0); + + for i in 0..n { + row_mask |= board[0][i] << i; + col_mask |= board[i][0] << i; + } + + let reverse_row_mask = ((1 << n) - 1) ^ row_mask; + let reverse_col_mask = ((1 << n) - 1) ^ col_mask; + + let (mut row_count, mut col_count) = (0, 0); + + for i in 0..n { + let (mut current_row_mask, mut current_col_mask) = (0, 0); + + for j in 0..n { + current_row_mask |= board[i][j] << j; + current_col_mask |= board[j][i] << j; + } + + if current_row_mask != row_mask && current_row_mask != reverse_row_mask { + return -1; + } + if current_row_mask == row_mask { + row_count += 1; + } + + if current_col_mask != col_mask && current_col_mask != reverse_col_mask { + return -1; + } + if current_col_mask == col_mask { + col_count += 1; + } + } + + let row_moves = Self::get_moves(row_mask, row_count, n as i32); + let col_moves = Self::get_moves(col_mask, col_count, n as i32); + if row_moves == -1 || col_moves == -1 { + -1 + } else { + row_moves + col_moves + } + } + + fn get_moves(mask: i32, count: i32, n: i32) -> i32 { + let one_count = mask.count_ones() as i32; + + if n % 2 == 0 { + if one_count != n >> 1 || count != n >> 1 { + return -1; + } + + let count0 = n / 2 - (mask & 0xAAAAAAAAu32 as i32).count_ones() as i32; + let count1 = n / 2 - (mask & 0x55555555).count_ones() as i32; + + count0.min(count1) + } else { + if n.abs_diff(2 * one_count) != 1 || n.abs_diff(2 * count) != 1 { + return -1; + } + + if one_count == n / 2 { + n / 2 - (mask & 0xAAAAAAAAu32 as i32).count_ones() as i32 + } else { + (n + 1) / 2 - (mask & 0x55555555).count_ones() as i32 + } + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_782() { + assert_eq!( + 2, + Solution::moves_to_chessboard(vec![ + vec![0, 1, 1, 0], + vec![0, 1, 1, 0], + vec![1, 0, 0, 1], + vec![1, 0, 0, 1] + ]) + ); + } +}