Facebook Pixel

3001. Minimum Moves to Capture The Queen

MediumMathEnumeration
Leetcode Link

Problem Description

You have an 8x8 chessboard with three pieces on it: a white rook, a white bishop, and a black queen. The positions are given as 1-indexed coordinates where (a, b) is the white rook's position, (c, d) is the white bishop's position, and (e, f) is the black queen's position.

Your goal is to find the minimum number of moves needed to capture the black queen using only the white pieces (rook or bishop). The black queen remains stationary throughout the game.

The pieces move according to standard chess rules:

  • Rook: Can move any number of squares horizontally or vertically in a straight line, but cannot jump over other pieces
  • Bishop: Can move any number of squares diagonally, but cannot jump over other pieces
  • Either piece can capture the queen by moving to the square where the queen is located

The key insight is determining whether you can capture the queen in 1 move or need 2 moves. You can capture in 1 move if:

  1. The rook is in the same row as the queen (and the bishop isn't blocking the path)
  2. The rook is in the same column as the queen (and the bishop isn't blocking the path)
  3. The bishop is on the same diagonal as the queen (and the rook isn't blocking the path)

If none of these direct capture conditions are met, you'll need exactly 2 moves to capture the queen - either by repositioning one piece to get a clear path, or by using coordinated moves of both pieces.

Quick Interview Experience
Help others by sharing your interview experience
Have you seen this problem before?

Intuition

The key observation is that we only need to determine if we can capture the queen in 1 move or 2 moves. Why is this?

First, let's think about when we can capture in 1 move. This happens when either the rook or bishop has a clear path to the queen:

  • The rook can reach the queen if they share the same row or column
  • The bishop can reach the queen if they're on the same diagonal

But there's a catch - the other piece might be blocking the path! So we need to check if the path is clear.

For the rook attacking horizontally (same row a == e):

  • The bishop blocks if it's also in row a AND between the rook and queen
  • We check this with (c != a or (d - b) * (d - f) > 0) - either the bishop isn't in the same row, or if it is, it's not between them (the product is positive when d is on the same side of both b and f)

Similar logic applies for vertical attacks and the bishop's diagonal attacks.

Now, why is 2 moves always sufficient if we can't capture in 1? Consider these strategies:

  • The rook can always reach the queen in 2 moves: move to the queen's row first, then to the queen's column (or vice versa)
  • The bishop can move to a diagonal that contains the queen, then capture
  • Even if one piece is initially blocked, it can move out of the way in the first move, allowing the other piece to capture in the second move

Since we can always capture in at most 2 moves, and we've identified all cases where we can capture in 1 move, the answer is either 1 or 2. This makes the solution straightforward: check all four 1-move capture conditions, and if none apply, return 2.

Learn more about Math patterns.

Solution Approach

Based on our analysis, we need to check four conditions for a 1-move capture. If any condition is true, return 1; otherwise, return 2.

Condition 1: Rook captures horizontally (same row)

if a == e and (c != a or (d - b) * (d - f) > 0):
    return 1
  • Check if rook and queen are in the same row: a == e
  • Ensure bishop doesn't block: either bishop is in a different row c != a, OR if in the same row, check it's not between them using (d - b) * (d - f) > 0
  • The product is positive when the bishop's column d is on the same side relative to both the rook's column b and queen's column f

Condition 2: Rook captures vertically (same column)

if b == f and (d != b or (c - a) * (c - e) > 0):
    return 1
  • Check if rook and queen are in the same column: b == f
  • Ensure bishop doesn't block: either bishop is in a different column d != b, OR if in the same column, it's not between them

Condition 3: Bishop captures on main diagonal \

if c - e == d - f and (a - e != b - f or (a - c) * (a - e) > 0):
    return 1
  • Check if bishop and queen are on the same diagonal: c - e == d - f (same row-column difference)
  • Ensure rook doesn't block: either rook is not on this diagonal a - e != b - f, OR if on the diagonal, it's not between them

Condition 4: Bishop captures on anti-diagonal /

if c - e == f - d and (a - e != f - b or (a - c) * (a - e) > 0):
    return 1
  • Check if bishop and queen are on the same anti-diagonal: c - e == f - d (row difference equals negative column difference)
  • Ensure rook doesn't block the path

If none of these conditions are met, we need 2 moves to capture the queen, so we return 2.

The elegance of this solution lies in using the sign of products to determine if a piece is between two others on the same line - a positive product means the blocking piece is outside the interval between the attacking piece and the queen.

Ready to land your dream job?

Unlock your dream job with a 5-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through an example where the rook is at position (1, 1), bishop at (4, 3), and queen at (5, 3).

Step 1: Check if rook can capture horizontally

  • Are rook and queen in same row? Check if a == e: 1 ≠ 5, so NO

Step 2: Check if rook can capture vertically

  • Are rook and queen in same column? Check if b == f: 1 ≠ 3, so NO

Step 3: Check if bishop can capture on main diagonal

  • Are bishop and queen on same diagonal? Check if c - e == d - f:
    • 4 - 5 = -1 and 3 - 3 = 0
    • -1 ≠ 0, so NO

Step 4: Check if bishop can capture on anti-diagonal

  • Are bishop and queen on same anti-diagonal? Check if c - e == f - d:
    • 4 - 5 = -1 and 3 - 3 = 0
    • -1 ≠ 0, so NO

Since none of the 1-move capture conditions are met, we return 2 moves.

Indeed, the rook needs 2 moves: first move to row 5 (e.g., to position (5, 1)), then move horizontally to capture the queen at (5, 3).


Let's try another example where we CAN capture in 1 move: rook at (1, 8), bishop at (3, 4), queen at (1, 4).

Step 1: Check if rook can capture horizontally

  • Are rook and queen in same row? Check if a == e: 1 == 1, YES!
  • Is the path clear? Check if bishop blocks:
    • Is bishop in row 1? Check c != a: 3 ≠ 1, so bishop is NOT in row 1
    • Since bishop isn't even in the same row, the path is clear!

We can capture in 1 move - the rook moves horizontally from (1, 8) to (1, 4) to capture the queen.

Solution Implementation

1class Solution:
2    def minMovesToCaptureTheQueen(
3        self, a: int, b: int, c: int, d: int, e: int, f: int
4    ) -> int:
5        """
6        Finds minimum moves to capture the queen on a chess board.
7      
8        Args:
9            a, b: Rook's position (row, column)
10            c, d: Bishop's position (row, column)
11            e, f: Queen's position (row, column)
12          
13        Returns:
14            Minimum number of moves (1 or 2) to capture the queen
15        """
16        # Rook coordinates
17        rook_row, rook_col = a, b
18        # Bishop coordinates  
19        bishop_row, bishop_col = c, d
20        # Queen coordinates
21        queen_row, queen_col = e, f
22      
23        # Check if rook can capture queen in one move (same row)
24        # Rook and queen are on same row AND bishop doesn't block the path
25        if rook_row == queen_row and (
26            bishop_row != rook_row or 
27            (bishop_col - rook_col) * (bishop_col - queen_col) > 0
28        ):
29            return 1
30      
31        # Check if rook can capture queen in one move (same column)
32        # Rook and queen are on same column AND bishop doesn't block the path
33        if rook_col == queen_col and (
34            bishop_col != rook_col or 
35            (bishop_row - rook_row) * (bishop_row - queen_row) > 0
36        ):
37            return 1
38      
39        # Check if bishop can capture queen in one move (main diagonal)
40        # Bishop and queen are on same main diagonal AND rook doesn't block the path
41        if bishop_row - queen_row == bishop_col - queen_col and (
42            rook_row - queen_row != rook_col - queen_col or 
43            (rook_row - bishop_row) * (rook_row - queen_row) > 0
44        ):
45            return 1
46      
47        # Check if bishop can capture queen in one move (anti-diagonal)
48        # Bishop and queen are on same anti-diagonal AND rook doesn't block the path
49        if bishop_row - queen_row == queen_col - bishop_col and (
50            rook_row - queen_row != queen_col - rook_col or 
51            (rook_row - bishop_row) * (rook_row - queen_row) > 0
52        ):
53            return 1
54      
55        # If no one-move capture is possible, it takes 2 moves
56        return 2
57
1class Solution {
2    public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
3        // Positions: Rook at (a,b), Bishop at (c,d), Queen at (e,f)
4      
5        // Check if rook can capture queen in one move
6      
7        // Case 1: Rook and queen are on the same row
8        // The rook can capture if bishop is not blocking the path
9        if (a == e && (c != a || (d - b) * (d - f) > 0)) {
10            return 1;
11        }
12      
13        // Case 2: Rook and queen are on the same column  
14        // The rook can capture if bishop is not blocking the path
15        if (b == f && (d != b || (c - a) * (c - e) > 0)) {
16            return 1;
17        }
18      
19        // Check if bishop can capture queen in one move
20      
21        // Case 3: Bishop and queen are on the same diagonal (slope = 1)
22        // The bishop can capture if rook is not blocking the diagonal path
23        if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) {
24            return 1;
25        }
26      
27        // Case 4: Bishop and queen are on the same diagonal (slope = -1)
28        // The bishop can capture if rook is not blocking the diagonal path
29        if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) {
30            return 1;
31        }
32      
33        // If no direct capture is possible, it takes 2 moves
34        // (One piece moves to create a path, then captures)
35        return 2;
36    }
37}
38
1class Solution {
2public:
3    int minMovesToCaptureTheQueen(int rookRow, int rookCol, int bishopRow, int bishopCol, int queenRow, int queenCol) {
4        // Check if rook can capture queen in one move (horizontal attack)
5        // Rook and queen are in same row, and bishop is not blocking
6        if (rookRow == queenRow && 
7            (bishopRow != rookRow || (bishopCol - rookCol) * (bishopCol - queenCol) > 0)) {
8            return 1;
9        }
10      
11        // Check if rook can capture queen in one move (vertical attack)
12        // Rook and queen are in same column, and bishop is not blocking
13        if (rookCol == queenCol && 
14            (bishopCol != rookCol || (bishopRow - rookRow) * (bishopRow - queenRow) > 0)) {
15            return 1;
16        }
17      
18        // Check if bishop can capture queen in one move (main diagonal attack)
19        // Bishop and queen are on same diagonal (row diff = col diff)
20        // and rook is not blocking on the same diagonal
21        if (bishopRow - queenRow == bishopCol - queenCol && 
22            (rookRow - queenRow != rookCol - queenCol || (rookRow - bishopRow) * (rookRow - queenRow) > 0)) {
23            return 1;
24        }
25      
26        // Check if bishop can capture queen in one move (anti-diagonal attack)
27        // Bishop and queen are on same anti-diagonal (row diff = -col diff)
28        // and rook is not blocking on the same anti-diagonal
29        if (bishopRow - queenRow == queenCol - bishopCol && 
30            (rookRow - queenRow != queenCol - rookCol || (rookRow - bishopRow) * (rookRow - queenRow) > 0)) {
31            return 1;
32        }
33      
34        // If no single move capture is possible, it takes 2 moves
35        // (one piece moves to attack position, then captures)
36        return 2;
37    }
38};
39
1/**
2 * Calculates the minimum number of moves to capture the queen on a chess board
3 * @param a - Row position of the rook
4 * @param b - Column position of the rook
5 * @param c - Row position of the bishop
6 * @param d - Column position of the bishop
7 * @param e - Row position of the queen
8 * @param f - Column position of the queen
9 * @returns The minimum number of moves (1 or 2) to capture the queen
10 */
11function minMovesToCaptureTheQueen(
12    a: number,
13    b: number,
14    c: number,
15    d: number,
16    e: number,
17    f: number
18): number {
19    // Check if rook can capture queen in one move (same row)
20    // Rook and queen are on the same row, and bishop is not blocking the path
21    if (a === e && (c !== a || (d - b) * (d - f) > 0)) {
22        return 1;
23    }
24  
25    // Check if rook can capture queen in one move (same column)
26    // Rook and queen are on the same column, and bishop is not blocking the path
27    if (b === f && (d !== b || (c - a) * (c - e) > 0)) {
28        return 1;
29    }
30  
31    // Check if bishop can capture queen in one move (main diagonal)
32    // Bishop and queen are on the same diagonal (row diff = col diff)
33    // and rook is not blocking the diagonal path
34    if (c - e === d - f && (a - e !== b - f || (a - c) * (a - e) > 0)) {
35        return 1;
36    }
37  
38    // Check if bishop can capture queen in one move (anti-diagonal)
39    // Bishop and queen are on the same anti-diagonal (row diff = -col diff)
40    // and rook is not blocking the diagonal path
41    if (c - e === f - d && (a - e !== f - b || (a - c) * (a - e) > 0)) {
42        return 1;
43    }
44  
45    // If no piece can capture in one move, it takes 2 moves
46    return 2;
47}
48

Time and Space Complexity

The time complexity is O(1) because the function performs a fixed number of conditional checks and arithmetic operations regardless of the input values. Each if statement contains constant-time comparisons and arithmetic operations (equality checks, subtraction, multiplication, and logical operations), and there are exactly 4 if statements that are evaluated sequentially. No loops or recursive calls are present, so the execution time remains constant.

The space complexity is O(1) because the function only uses the six input parameters (a, b, c, d, e, f) and does not allocate any additional data structures, arrays, or variables. The space usage remains constant regardless of the input values, as only a fixed amount of memory is needed to store the input parameters and perform the arithmetic operations.

Learn more about how to find time and space complexity quickly.

Common Pitfalls

1. Incorrect Blocking Logic for Diagonal Moves

The Pitfall: A common mistake is incorrectly determining whether a piece blocks a diagonal path. For diagonals, simply checking if a piece is on the same diagonal isn't enough - you need to verify if it's actually between the attacking piece and the target.

Incorrect approach:

# Wrong: Only checks if rook is on the diagonal, not if it's blocking
if bishop_row - queen_row == bishop_col - queen_col and (rook_row - queen_row == rook_col - queen_col):
    return 2  # Assumes blocking without checking position

Correct approach:

# Right: Checks if rook is on diagonal AND between bishop and queen
if bishop_row - queen_row == bishop_col - queen_col and (
    rook_row - queen_row != rook_col - queen_col or  # Not on diagonal
    (rook_row - bishop_row) * (rook_row - queen_row) > 0  # Or not between them
):
    return 1

2. Confusing Main Diagonal vs Anti-Diagonal Conditions

The Pitfall: Mixing up the mathematical conditions for main diagonal (\) and anti-diagonal (/) alignment.

  • Main diagonal: Points where row - col is constant, checked by c - e == d - f
  • Anti-diagonal: Points where row + col is constant, checked by c - e == f - d

Incorrect approach:

# Wrong: Uses same condition for both diagonals
if c - e == d - f:  # This only works for main diagonal
    # Check both diagonals...

Correct approach:

# Main diagonal check
if c - e == d - f:
    # Bishop can attack along \ diagonal
  
# Anti-diagonal check (note the sign difference)
if c - e == f - d:  # Equivalent to c + d == e + f
    # Bishop can attack along / diagonal

3. Off-by-One Errors in the Blocking Check

The Pitfall: Using incorrect inequality signs when checking if a piece is between two others. The product method (pos - start) * (pos - end) > 0 works because:

  • If the piece is between start and end, one difference is positive and one is negative, giving a negative product
  • If the piece is outside the interval, both differences have the same sign, giving a positive product

Incorrect approach:

# Wrong: Uses <= 0 which would allow the blocking piece at the same position
if (bishop_col - rook_col) * (bishop_col - queen_col) <= 0:
    return 2  # Blocked

Correct approach:

# Right: Uses > 0 to check if bishop is NOT between rook and queen
if (bishop_col - rook_col) * (bishop_col - queen_col) > 0:
    return 1  # Not blocked, can capture

4. Forgetting Edge Cases Where Pieces Share Positions

The Pitfall: Not handling cases where two pieces might be on the same square (though this shouldn't happen in valid chess positions, the problem might not explicitly forbid it).

Solution: Add validation at the beginning if needed:

# Optional validation
if (a, b) == (c, d) or (a, b) == (e, f) or (c, d) == (e, f):
    # Handle invalid position or immediate capture scenario
    pass

5. Overcomplicating the Two-Move Case

The Pitfall: Trying to calculate the exact sequence of two moves when the problem only asks for the minimum number.

Key insight: If you can't capture in 1 move, you can always capture in exactly 2 moves:

  • Move 1: Position a piece to threaten the queen
  • Move 2: Capture the queen

There's no need to determine which piece moves or the exact path - just return 2 if no 1-move capture exists.

Discover Your Strengths and Weaknesses: Take Our 5-Minute Quiz to Tailor Your Study Plan:

Which of the following array represent a max heap?


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!

Load More