Determine the minimum number of bishop moves needed to move from one square to another on a chessboard, or report that it is impossible.
Problem
You are given a chessboard coordinate system and two squares: a starting square and a target square. A bishop moves diagonally, and in one move it may travel any distance along a diagonal.
Return the minimum number of bishop moves required to reach the target square from the starting square.
If the target cannot be reached by any sequence of legal bishop moves, return -1.
Notes
- A bishop always stays on squares of the same color.
- A single bishop move changes both row and column by the same amount in absolute value.
- You may assume the board is large enough for the movement rules to be the only constraint unless stated otherwise by the caller.
Input Format
- Two squares, usually represented as coordinates
(r1, c1)and(r2, c2). - Coordinates are integers.
Output Format
- Return an integer:
0if start and target are the same square.1if the target is reachable in one bishop move.2if the target is reachable but requires two moves.-1if the target is unreachable.
Constraints
- A bishop moves only diagonally.
- Squares on different colors are unreachable.
- If the board boundaries are relevant, the move must remain on the board.
Example 1
Input
start = (0, 0), target = (2, 2)
Output
1
Explanation
The target lies on the same diagonal as the start, so one bishop move is enough.
Example 2
Input
start = (0, 0), target = (1, 2)
Output
-1
Explanation
The two squares have different colors, so a bishop can never move between them.
Show 1 more example
Example 3
Input
start = (0, 0), target = (1, 1)
Output
2
Explanation
The squares have the same color, but they are not on the same diagonal. A bishop can reach the target in two moves via an intermediate square.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.