Minimum Bishop Moves to Reach Target - Solution & Explanation
Practice this problemProblem Statement
There is an 8 x 8 empty chessboard with 1-indexed rows and columns.
You are given an array source = [sr, sc] representing the starting position of a bishop, and an array target = [tr, tc]. In one move, the bishop travels any number of squares along a single diagonal direction, staying within the board.
Return the minimum number of moves for the bishop to land exactly on target. If it can never reach target, return -1.
Example 1:
Input: source = [8,1], target = [1,8]
Output: 1
Explanation:
āāāāāāā
A single diagonal move takes the bishop straight from (8, 1) to (1, 8).
Example 2:
Input: source = [4,2], target = [1,3]
Output: 2
Explanation:

The bishop moves from (4, 2) to (3, 1), then from (3, 1) to (1, 3), reaching the target in 2 moves.
Example 3:
Input: source = [1,1], target = [3,4]
Output: -1
Explanation:
No matter how many diagonal moves it makes, the bishop starting at (1, 1) can never land on (3, 4). Thus, the answer is -1.
Constraints:āāāāāāā
source.length == target.length == 21 <= sr, sc, tr, tc <= 8source != target
Approach Overview
Problem Overview: Given start and target positions on a standard 8x8 chessboard, find the minimum number of moves a bishop needs to reach the target. A bishop moves diagonally any number of squares.
Approach 1: Mathematical (O(1) time, O(1) space)
Check if the squares are the same color. If not, return -1. If same square, return 0. If they are on the same diagonal (absolute difference of rows equals absolute difference of columns), return 1. Otherwise, return 2. This works because any two same-colored squares can be connected via a single intermediate square that lies on both diagonals.
Approach 2: BFS (O(1) time, O(1) space)
Since the board is 8x8, BFS from start to target exploring all diagonal moves also works. The board is small, so BFS runs in constant time. However, the mathematical approach is simpler and more efficient in practice. BFS is more general for arbitrary board sizes but is overkill here.
Recommended for interviews: Use the mathematical approach for an O(1) solution. It is the expected answer in interviews. BFS shows understanding of graph traversal, but the problem is designed to test math reasoning. The brute force BFS demonstrates basic skills, but the optimal math solution shows insight.
Related topics: Math, BFS, Chessboard
Solution
A bishop only moves along diagonals, and each move changes the row and the column by the same amount, so (r + c) bmod 2 never changes. In other words, the bishop can only stand on squares of the same color as its starting square. If (sr + sc) and (tr + tc) have different parities, the bishop can never reach the target, so we return -1.
Otherwise, if the source and the target lie on the same diagonal, i.e., |sr - tr| = |sc - tc|, a single move is enough, so we return 1.
In all remaining cases, the two squares share the same color but are not on a common diagonal. Since source neq target is guaranteed and any two same-colored squares on an 8 times 8 board can be joined through some intermediate square, the answer is 2.
The time complexity is O(1), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Mathematical | O(1) | O(1) | Always preferred for this problem |
| BFS | O(1) (constant board) | O(1) | When board size is variable or for learning graph traversal |
Video Solutions (2)
LeetCode Biweekly Contest 190 š„ | 3 Problems Solved | Q1āQ3 | 4034, 4035, 4036
Frequently Asked Questions
Is Minimum Bishop Moves to Reach Target easy or hard?
Minimum Bishop Moves to Reach Target Python/Java solution
How to solve Minimum Bishop Moves to Reach Target in O(n)?
What is the best approach for Minimum Bishop Moves to Reach Target?
Is Minimum Bishop Moves to Reach Target asked at Google/Amazon/Meta?
What data structure is used in Minimum Bishop Moves to Reach Target?
What is the time complexity of Minimum Bishop Moves to Reach Target?
Ready to solve this problem?
Practice Minimum Bishop Moves to Reach Target with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor