Skip to main content

Minimum Moves to Capture The Queen - Solution & Explanation

MediumArrayEnumeration18 min readAsked at: Goldman Sachs, Wipro
Practice this problem

Problem Statement

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.

 

Example 1:

Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
Output: 2
Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.

Example 2:

Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
Output: 1
Explanation: We can capture the black queen in a single move by doing one of the following: 
- Move the white rook to (5, 2).
- Move the white bishop to (5, 2).

 

Constraints:

  • 1 <= a, b, c, d, e, f <= 8
  • No two pieces are on the same square.

Approach Overview

Problem Overview: You are given coordinates of a rook, a bishop, and a queen on an 8x8 chessboard. The goal is to determine the minimum number of moves required for either the rook or the bishop to capture the queen while respecting normal chess movement rules and piece blocking.

Approach 1: Breadth-First Search (BFS) Enumeration (Time: O(1), Space: O(1))

This approach models the chessboard as a small state space and explores possible moves using Breadth-First Search. From the rook and bishop positions, generate all legal moves according to their movement rules and check whether the queen can be captured. BFS guarantees the shortest path because moves are explored level by level. The board size is fixed (8x8), so the number of states is constant, which keeps the complexity effectively O(1). This approach is useful if you want a generic framework that can be extended to other chess‑like movement problems.

To implement it, enqueue the current piece position and simulate each legal direction step by step until the edge of the board or a blocking piece is reached. If the queen appears along that path, you can capture in one move. Otherwise, continue exploring positions reachable in the next move. Because the board is tiny, BFS quickly terminates.

Approach 2: Direct Simulation with Pruning (Time: O(1), Space: O(1))

This method relies on direct geometric checks instead of exploring moves. The key observation: the answer can only be 1 or 2. A capture happens in one move if either the rook or bishop already has a clear attacking line to the queen. For the rook, check if they share the same row or column. Then verify the bishop is not positioned between them on that line. For the bishop, check if the queen lies on the same diagonal and ensure the rook is not blocking that diagonal path.

If any valid attacking line exists without obstruction, return 1. Otherwise return 2, since one piece can reposition on the first move and capture on the second. These checks are simple coordinate comparisons and conditional logic, which makes the algorithm constant time.

The solution is essentially a small enumeration of chess attack patterns using basic coordinate math. Even though the board could be represented with an array, the optimal solution avoids building a board entirely and instead works directly with positions.

Recommended for interviews: Direct simulation with pruning is what interviewers usually expect. It shows you recognize rook/diagonal attack patterns and handle blocking conditions cleanly in constant time. BFS still demonstrates problem‑solving ability and systematic exploration, but the direct check proves stronger pattern recognition and leads to a simpler O(1) implementation.

Approach 1: Breadth-First Search (BFS)

To determine the minimum number of moves required to capture the black queen, we can utilize a Breadth-First Search (BFS) approach. The idea is to explore all possible moves from both the rook and bishop simultaneously, updating and checking each piece's potential to capture the queen at every step. Starting from their initial positions, simulate the movements of the rook and bishop on the chessboard, adding each reachable position to a queue to explore subsequent moves. If a piece reaches the queen's position, return the number of moves required. This ensures the shortest path due to the nature of BFS.

This Python solution employs BFS to determine the minimum number of moves required by simulating the rook and bishop moves on the chessboard. The positions are tracked using a queue, and every possible move is explored until one of the pieces captures the queen.

Code

Python

JavaScript

Complexity

Time Complexity: O(1), because the chessboard size is fixed (8x8).
Space Complexity: O(1), due to limited storage of board positions.

Try this approach in the editor →

Approach 2: Direct Simulation with Pruning

This approach directly simulates the movement of the rook and bishop toward the queen while applying pruning strategies to skip unnecessary explorations. By examining direct lines of attack first and pruning paths that cannot reach the queen due to other pieces, this method aims for optimal move count calculations.

This C++ solution directly assesses whether the piece can 'see' the queen and captures it efficiently with 1 move. Otherwise, it calculates the shortest path using movement rules and constraints, avoiding unnecessary paths.

Code

C++

Java

Complexity

Time Complexity: O(1), since computations are constant time operations.
Space Complexity: O(1), due to basic variable usage only.

Try this approach in the editor →

Approach 3: Case Analysis

According to the problem description, we can categorize the scenarios for capturing the black queen as follows:

  1. The white rook and the black queen are in the same row with no other pieces in between. In this case, the white rook only needs to move once.
  2. The white rook and the black queen are in the same column with no other pieces in between. In this case, the white rook only needs to move once.
  3. The white bishop and the black queen are on the same diagonal \ with no other pieces in between. In this case, the white bishop only needs to move once.
  4. The white bishop and the black queen are on the same diagonal / with no other pieces in between. In this case, the white bishop only needs to move once.
  5. In other cases, only two moves are needed.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Cangjie

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Breadth-First Search (BFS)

Time Complexity: O(1), because the chessboard size is fixed (8x8).
Space Complexity: O(1), due to limited storage of board positions.

Direct Simulation with Pruning

Time Complexity: O(1), since computations are constant time operations.
Space Complexity: O(1), due to basic variable usage only.

Case Analysis

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Breadth-First Search (BFS)O(1)O(1)When modeling the board as a state graph or extending to more complex movement rules
Direct Simulation with PruningO(1)O(1)Best for interviews; uses coordinate math and blocking checks for immediate evaluation

Video Solution

Minimum Moves to Capture The Queen | All Diagram CasesAryan Mittal2,360 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Minimum Moves to Capture The Queen easy or hard?
The problem is rated Medium because the logic involves multiple geometric conditions and blocking scenarios. Once you recognize the rook and bishop attack patterns and handle the blocking checks correctly, the implementation becomes short and runs in constant time.
Minimum Moves to Capture The Queen Python/Java solution
Python and Java implementations typically follow the same constant‑time logic: check rook alignment with the queen, verify no blocking by the bishop, then check bishop diagonal alignment and ensure the rook does not block the path. The code mainly consists of coordinate comparisons and simple conditional statements.
How to solve Minimum Moves to Capture The Queen in O(1)?
Check two attack conditions. First, verify whether the rook and queen share the same row or column and ensure the bishop is not positioned between them on that line. Second, verify whether the bishop and queen lie on the same diagonal and ensure the rook does not block the diagonal. If either condition is satisfied, return 1; otherwise return 2.
What is the best approach for Minimum Moves to Capture The Queen?
Direct simulation with pruning is the best approach. Instead of exploring moves, check if the rook shares the same row or column with the queen or if the bishop shares the same diagonal. If the attacking line is not blocked by the other piece, the queen can be captured in one move; otherwise the answer is two. This method runs in O(1) time and uses O(1) space.
Is Minimum Moves to Capture The Queen asked at Google/Amazon/Meta?
Chessboard geometry and coordinate simulation problems appear frequently in technical interviews at companies like Google, Amazon, and Meta. Variations involving rook, bishop, or queen movement are common because they test logical reasoning, edge‑case handling, and constant‑time optimization.
What data structure is used in Minimum Moves to Capture The Queen?
The optimal approach does not require a complex data structure. It mainly uses coordinate comparisons and conditional checks. A BFS version may conceptually treat the board as a grid or array, but the direct simulation solution avoids building an explicit board structure.
What is the time complexity of Minimum Moves to Capture The Queen?
The optimal solution runs in O(1) time and O(1) space because it only performs a few coordinate comparisons and blocking checks. Even a BFS-based solution remains effectively constant time since the chessboard size is fixed at 8x8 and the number of states is very small.

Ready to solve this problem?

Practice Minimum Moves to Capture The Queen with our built-in code editor and test cases.

Practice on FleetCode