From d7b3b2a116e91833f86cadc69e6bfc80acbd5341 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 7 Dec 2024 12:17:37 +0800 Subject: [PATCH] 20241207 finished. --- src/problem/mod.rs | 2 + .../p688_knight_probability_in_chessboard.rs | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/problem/p688_knight_probability_in_chessboard.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index bcf1d52..afd8daa 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -354,3 +354,5 @@ mod p2056_number_of_valid_move_combinations_on_chessboard; mod p3001_minimum_moves_to_capture_the_queen; mod p999_available_captures_for_rook; + +mod p688_knight_probability_in_chessboard; diff --git a/src/problem/p688_knight_probability_in_chessboard.rs b/src/problem/p688_knight_probability_in_chessboard.rs new file mode 100644 index 0000000..8e19d7c --- /dev/null +++ b/src/problem/p688_knight_probability_in_chessboard.rs @@ -0,0 +1,62 @@ +/** + * [688] Knight Probability in Chessboard + */ +pub struct Solution {} + +// submission codes start here + +const DIRECTIONS: [(i32, i32); 8] = [ + (-1, -2), + (1, -2), + (-1, 2), + (1, 2), + (-2, -1), + (2, -1), + (-2, 1), + (2, 1), +]; + +impl Solution { + pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 { + let (n, k) = (n as usize, k as usize); + let (row, column) = (row as usize, column as usize); + + let mut dp = vec![vec![vec![0f64; n]; n]; k + 1]; + + for step in 0..=k { + for x in 0..n { + for y in 0..n { + if step == 0 { + dp[step][x][y] = 1f64; + } else { + for &(dx, dy) in DIRECTIONS.iter() { + let nx = x as i32 + dx; + let ny = y as i32 + dy; + + if nx >= 0 && nx < n as i32 && ny >= 0 && ny < n as i32 { + let (nx, ny) = (nx as usize, ny as usize); + + dp[step][x][y] += dp[step - 1][nx][ny] / 8f64; + } + } + } + } + } + } + + dp[k][row][column] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_688() { + assert_eq!(0.0625, Solution::knight_probability(3, 2, 0, 0)); + assert_eq!(1.0, Solution::knight_probability(1, 0, 0, 0)); + } +}