Minimum Moves to Capture The Queen - Problem

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.

Input & Output

Example 1 — Rook Direct Attack
$ Input: a=1, b=1, c=3, d=3, e=1, f=8
Output: 1
💡 Note: Rook at (1,1) and Queen at (1,8) are on the same row. Bishop at (3,3) doesn't block the path, so rook can capture queen in 1 move.
Example 2 — Bishop Blocked
$ Input: a=2, b=2, c=1, d=4, e=4, f=1
Output: 2
💡 Note: Bishop at (1,4) can attack diagonally to (4,1), but rook at (2,2) is not on the same diagonal. Neither piece can capture in 1 move, so answer is 2.
Example 3 — Bishop Direct Attack
$ Input: a=5, b=5, c=2, d=2, e=4, f=4
Output: 1
💡 Note: Bishop at (2,2) and Queen at (4,4) are on the same diagonal. Rook at (5,5) doesn't block this diagonal path, so bishop captures in 1 move.

Constraints

  • 1 ≤ a, b, c, d, e, f ≤ 8
  • No two pieces occupy the same square
  • All coordinates represent valid chessboard positions

Visualization

Tap to expand
Chess Attack Problem: Find Minimum MovesRook (1,1)Bishop (3,2)Queen (6,1)Can rook attack queen directly? Same row, not blocked → YESCan bishop attack queen directly? Different diagonal → NOResult: 1 move (rook captures queen)
Understanding the Visualization
1
Input
Rook at (a,b), Bishop at (c,d), Queen at (e,f)
2
Analysis
Check if rook or bishop can attack queen directly
3
Output
Return 1 if direct attack possible, otherwise 2
Key Takeaway
🎯 Key Insight: Either a piece can capture immediately (1 move) or it always takes exactly 2 moves
Asked in
Google 15 Microsoft 12 Amazon 8
18.5K Views
Medium Frequency
~15 min Avg. Time
420 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen