Problem Description
There is an 8x8 chessboard with three pieces: a white rook, a white bishop, and a black queen. Given their positions, you are allowed to move only the white pieces following standard chess rules (note that they cannot jump over other pieces). Determine the minimum number of moves needed to capture the queen.
Key Insights
- A piece can capture the queen if the queen lies on a valid move path of that piece (i.e. same row/column for the rook and same diagonal for the bishop) and the path between the piece and the queen is unobstructed by the other white piece.
- Check if either white piece can capture the queen in one move by ensuring:
- For the rook: either the row or the column is shared and the bishop does not block the path.
- For the bishop: the queen is on the same diagonal and the rook does not lie in between.
- If a one move solution isn’t available, then a two move solution always exists because one can first reposition one piece to remove a blocking obstacle or to line up a direct strike.
Space and Time Complexity
Time Complexity: O(1) — the board is of constant size (8x8) and only a few arithmetic and comparison operations are performed. Space Complexity: O(1) — a constant amount of extra space is used.
Solution
The algorithm first checks if the rook or bishop can capture the queen in one move:
- For the rook: If the queen shares the same row or column, iterate through the squares between the rook and queen. If the bishop is present on this segment, the path is blocked.
- For the bishop: If the queen is diagonally aligned with the bishop, iterate along the diagonal path and check if the rook is between them. If either piece can capture the queen directly, the answer is 1. Otherwise, 2 moves are required—one move to reposition a piece to an unobstructed line or diagonal, and the second to capture the queen.
Key data structures and methods used:
- Simple arithmetic and iteration over a fixed set of squares.
- The checking functions for unobstructed paths ensure that there is no interference from the other white piece.