diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 26cb007..bcf1d52 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -352,3 +352,5 @@ mod p3274_check_if_two_chessboard_squares_have_the_same_color; mod p2056_number_of_valid_move_combinations_on_chessboard; mod p3001_minimum_moves_to_capture_the_queen; + +mod p999_available_captures_for_rook; diff --git a/src/problem/p999_available_captures_for_rook.rs b/src/problem/p999_available_captures_for_rook.rs new file mode 100644 index 0000000..653baef --- /dev/null +++ b/src/problem/p999_available_captures_for_rook.rs @@ -0,0 +1,106 @@ +/** + * [999] Available Captures for Rook + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn num_rook_captures(board: Vec>) -> i32 { + let n = board.len(); + let (x, y) = (0..n) + .map(|x| { + let y = board[x].iter().enumerate().find_map( + |(y, &c)| { + if c == 'R' { + Some(y) + } else { + None + } + }, + ); + + y.and_then(|y| Some((x, y))) + }) + .flatten() + .next() + .unwrap(); + + let mut result = 0; + + let mut dx = x; + while dx > 0 { + dx -= 1; + + match board[dx][y] { + 'p' => { + result += 1; + break; + } + 'B' => { + break; + } + _ => {} + } + } + + let mut dx = x; + while dx < n - 1 { + dx += 1; + + match board[dx][y] { + 'p' => { + result += 1; + break; + } + 'B' => { + break; + } + _ => {} + } + } + + let mut dy = y; + while dy > 0 { + dy -= 1; + + match board[x][dy] { + 'p' => { + result += 1; + break; + } + 'B' => { + break; + } + _ => {} + } + } + + let mut dy = y; + while dy < n - 1 { + dy += 1; + + match board[x][dy] { + 'p' => { + result += 1; + break; + } + 'B' => { + break; + } + _ => {} + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + + #[test] + fn test_999() {} +}