Minimum Knight Moves

Given a chessboard target coordinate, return the minimum number of knight moves needed to reach it from the origin. Pattern focus: Unweighted BFS distance. The knight move graph is unweighted, so BFS gives the shortest number of moves.

Input Format

x = target column, y = target row on an infinite chessboard

Output Format

minimum number of knight moves from (0,0) to (x,y)

Constraints

  • -300 <= x, y <= 300

Examples

Example 1:

Input:

x = 2
y = 1

Output:

1

Explanation:

A knight can reach (2,1) in one move.

Example 2:

Input:

x = 5
y = 5

Output:

4

Explanation:

BFS finds the minimum number of knight moves.

Example 3:

Input:

x = 0
y = 0

Output:

0

Explanation:

The origin is already the target.

Loading...
Minimum Knight Moves - Shortest Path DSA Problem