From 6762fefa22539c5e879e3fe981b7a49783742030 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Wed, 23 Oct 2024 22:49:38 +0800 Subject: [PATCH] Add problem 2257: Count Unguarded Cells in the Grid --- src/lib.rs | 1 + .../iterative.rs | 76 +++++++++++++++++++ .../mod.rs | 37 +++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/problem_2257_count_unguarded_cells_in_the_grid/iterative.rs create mode 100644 src/problem_2257_count_unguarded_cells_in_the_grid/mod.rs diff --git a/src/lib.rs b/src/lib.rs index e937bdb7..50c2ad69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1677,6 +1677,7 @@ pub mod problem_2250_count_number_of_rectangles_containing_each_point; pub mod problem_2251_number_of_flowers_in_full_bloom; pub mod problem_2255_count_prefixes_of_a_given_string; pub mod problem_2256_minimum_average_difference; +pub mod problem_2257_count_unguarded_cells_in_the_grid; pub mod problem_2258_escape_the_spreading_fire; pub mod problem_2259_remove_digit_from_number_to_maximize_result; pub mod problem_2260_minimum_consecutive_cards_to_pick_up; diff --git a/src/problem_2257_count_unguarded_cells_in_the_grid/iterative.rs b/src/problem_2257_count_unguarded_cells_in_the_grid/iterative.rs new file mode 100644 index 00000000..05c994f2 --- /dev/null +++ b/src/problem_2257_count_unguarded_cells_in_the_grid/iterative.rs @@ -0,0 +1,76 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn count_unguarded(m: i32, n: i32, guards: Vec>, walls: Vec>) -> i32 { + let m = m as u32 as usize; + let n = n as u32 as usize; + let mut grid = vec![0_u8; n * m].into_boxed_slice(); + + for (c, positions) in [(b'g', guards), (b'w', walls)] { + for position in positions { + let [y, x] = <[_; 2]>::map(position.try_into().ok().unwrap(), |x| x as u32 as usize); + + grid[n * y + x] = c; + } + } + + let mut guarded = vec![false; n].into_boxed_slice(); + + grid.chunks_exact_mut(n).for_each(|row| { + let mut left_guarded = false; + + row.iter_mut().zip(&mut *guarded).for_each(|(cell, top_guarded)| { + if *cell == 0 { + *cell = u8::from(left_guarded || *top_guarded); + } else { + let guarded = *cell == b'g'; + + left_guarded = guarded; + *top_guarded = guarded; + } + }); + }); + + guarded.fill(false); + + let mut result = 0; + + grid.chunks_exact(n).rev().for_each(|row| { + let mut right_guarded = false; + + row.iter() + .zip(&mut *guarded) + .rev() + .for_each(|(&cell, bottom_guarded)| match cell { + 0 => result += i32::from(!(right_guarded || *bottom_guarded)), + 1 => {} + _ => { + let guarded = cell == b'g'; + + right_guarded = guarded; + *bottom_guarded = guarded; + } + }); + }); + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn count_unguarded(m: i32, n: i32, guards: Vec>, walls: Vec>) -> i32 { + Self::count_unguarded(m, n, guards, walls) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2257_count_unguarded_cells_in_the_grid/mod.rs b/src/problem_2257_count_unguarded_cells_in_the_grid/mod.rs new file mode 100644 index 00000000..b1f4f6cb --- /dev/null +++ b/src/problem_2257_count_unguarded_cells_in_the_grid/mod.rs @@ -0,0 +1,37 @@ +pub mod iterative; + +pub trait Solution { + fn count_unguarded(m: i32, n: i32, guards: Vec>, walls: Vec>) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ( + ( + 4, + 6, + &[[0, 0], [1, 1], [2, 3]] as &[_], + &[[0, 1], [2, 2], [1, 4]] as &[_], + ), + 7, + ), + ((3, 3, &[[1, 1]], &[[0, 1], [1, 0], [2, 1], [1, 2]]), 4), + ]; + + for ((m, n, guards, walls), expected) in test_cases { + assert_eq!( + S::count_unguarded( + m, + n, + guards.iter().map(Vec::from).collect(), + walls.iter().map(Vec::from).collect(), + ), + expected, + ); + } + } +}