20241206 finished.

This commit is contained in:
jackfiled 2024-12-06 12:06:21 +08:00
parent 577b2f1733
commit beda526e1d
2 changed files with 108 additions and 0 deletions

View File

@ -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 p2056_number_of_valid_move_combinations_on_chessboard;
mod p3001_minimum_moves_to_capture_the_queen; mod p3001_minimum_moves_to_capture_the_queen;
mod p999_available_captures_for_rook;

View File

@ -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<Vec<char>>) -> 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() {}
}