3001. Minimum Moves to Capture The Queen
SourceWeekly Contest 379 Q2DifficultyMediumRating1796
Description
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.
Solutions
Solution 1: Case Analysis
Thinking
The board is \(8 \times 8\), so simulating rook and bishop paths is constant time. Each piece can reach any aligned square in at most one move, so the answer is at most \(2\).
The only one-move cases are a clear rook file/rank or a clear bishop diagonal to the queen.
Products such as \((d-b)(d-f)>0\) test that the blocking piece lies outside the open segment. If none of the four alignments is free, the rook captures in two moves.
According to the problem description, we can categorize the scenarios for capturing the black queen as follows:
- 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.
- 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.
- 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. - 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. - In other cases, only two moves are needed.
The time complexity is \(O(1)\), and the space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |

