#3001
Medium Algorithms Minimum moves to capture the queen
Math Enumeration
22.3% acceptance
Feb 25, 2026
183
209
There is a 1-indexed 8 x 8 chessboard containing 3 pieces.
You are given 6 integers a, b, c, d, e, and f where:
(a, b) denotes the position of the white rook.
(c, d) denotes the position of the white bishop.
(e, f) denotes the position of the black queen.
Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.
Note that:
Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
Bishops can move any number of squares diagonally, but cannot jump over other pieces.
A rook or a bishop can capture the queen if it is located in a square that they can move to.
The queen does not move.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {
// Rook same row, check bishop not blocking
if a == e {
let min_col = b.min(f);
let max_col = b.max(f);
if !(c == a && d > min_col && d < max_col) { return 1; }
}
// Rook same col
if b == f {
let min_row = a.min(e);
let max_row = a.max(e);
if !(d == b && c > min_row && c < max_row) { return 1; }
}
// Bishop same diagonal
if (c - e).abs() == (d - f).abs() {
let dr = (e - c).signum();
let dc = (f - d).signum();
let mut r = c + dr;
let mut col = d + dc;
let mut blocked = false;
while r != e || col != f {
if r == a && col == b { blocked = true; break; }
r += dr;
col += dc;
}
if !blocked { return 1; }
}
2
}
}