Knight Connection
Knight Connection
Category
Arrays
Difficulty
Hard
Problem Statement
Given the positions of two knights on an infinite chessboard, determine the minimum number of moves such that one knight can reach the other. Both knights move simultaneously, and each can make standard chess knight moves (an L-shape: 2 squares in one direction and 1 square perpendicular). Return the minimum number of turns needed for the two knights to land on the same square.
Intuition
Since both knights move simultaneously, each turn effectively covers twice the movement of a single knight. We can reformulate the problem: compute the minimum moves for a single knight to travel from one position to the other, then divide by 2 (rounding up) since both knights share the work. The single-knight minimum distance on an infinite board is well-studied and can be solved with BFS or a mathematical formula.
Approach
- Compute the absolute differences
dxanddybetween the two knight positions. - Use BFS from the origin
(0, 0)to reach(dx, dy)using knight moves, or use the mathematical approach: a. Due to symmetry, assumedx >= dy >= 0. b. Handle small special cases directly (e.g.,(1,0)requires 3 moves). c. For larger distances, the minimum single-knight moves can be computed with a known formula based on the geometry of knight moves. - Divide the single-knight move count by 2, rounding up:
result = ceil(singleKnightMoves / 2).
Pseudocode
function knightConnection(knightA, knightB):
dx = abs(knightA[0] - knightB[0])
dy = abs(knightA[1] - knightB[1])
// BFS approach for single knight minimum moves
singleMoves = bfsKnightDistance(dx, dy)
return ceil(singleMoves / 2)
function bfsKnightDistance(dx, dy):
queue = [(0, 0)]
visited = set containing (0, 0)
moves = 0
knightOffsets = [(-2,-1),(-2,1),(-1,-2),(-1,2),(1,-2),(1,2),(2,-1),(2,1)]
while queue is not empty:
for each (x, y) in current level of queue:
if x == dx and y == dy:
return moves
for (ox, oy) in knightOffsets:
nx, ny = x + ox, y + oy
if (nx, ny) not in visited and
nx >= -2 and ny >= -2 and
nx <= dx + 2 and ny <= dy + 2:
visited.add((nx, ny))
add (nx, ny) to next level
moves++
return -1 // unreachable
Time & Space Complexity
- Time: O(max(dx, dy)^2) for BFS in the worst case, though bounding the search space keeps it practical. A mathematical solution can achieve O(1).
- Space: O(max(dx, dy)^2) for BFS visited set. O(1) for the mathematical approach.
Key Insights
- Both knights moving simultaneously halves the effective distance. The answer is always
ceil(singleKnightDistance / 2). - The BFS search space can be bounded to a small region around the target, since a knight will never need to go far past the target to reach it optimally.
- Knight distance has interesting properties: for positions like
(1,0), the distance is 3 (not 1), because the L-shaped moves require detours.
Edge Cases
- Knights on the same square: 0 moves needed.
- Knights one step apart in a cardinal direction: requires multiple knight moves to resolve.
- Knights already a single knight move apart: 1 turn suffices (one knight moves to the other).
- Very large distances: BFS may be slow; a mathematical formula is preferred.
- Positions with
(2,2): requires 4 single-knight moves despite the small distance.