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: 1
💡 Note: Bishop at (1,4) and Queen at (4,1) are on the same diagonal (both differences are 3). Rook at (2,2) is not on this diagonal path, so bishop can capture queen in 1 move.
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
Minimum Moves to Capture The Queen INPUT R B Q 1 3 row 1 row 8 Rook: (a=1, b=1) Bishop: (c=3, d=3) Queen: (e=1, f=8) Rook Bishop Queen ALGORITHM STEPS 1 Check Rook Direct Same row OR same column? 2 Rook Path Analysis a=1, e=1 (same column!) 3 Check Blocking Bishop at c=3 (not in col 1) 4 Conclude Path clear, 1 move needed Mathematical Check: Rook col: a = 1 Queen col: e = 1 a == e? TRUE --> direct path! Bishop blocks? c != 1 --> NO FINAL RESULT Rook captures Queen R (1,1) Q (1,8) B (3,3) Output: 1 Minimum moves = 1 Rook moves vertically from (1,1) to (1,8) OK - 1 MOVE Key Insight: The rook can capture the queen in exactly 1 move when they share the same row OR column, AND no piece blocks the path. Here, rook at col 1 and queen at col 1 with clear vertical path. Otherwise, check bishop's diagonal attack or return 2 (rook can always reach in at most 2 moves). TutorialsPoint - Minimum Moves to Capture The Queen | Mathematical Analysis
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