Watch 10 video solutions for Minimum Moves to Capture The Queen, a medium level problem involving Array, Enumeration. This walkthrough by Aryan Mittal has 2,302 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
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 <= 8Problem 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 | Time | Space | When 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 Pruning | O(1) | O(1) | Best for interviews; uses coordinate math and blocking checks for immediate evaluation |