In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].
A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.
Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.
Example 1:
Input: x = 2, y = 1 Output: 1 Explanation: [0, 0] → [2, 1]
Example 2:
Input: x = 5, y = 5 Output: 4 Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]
Constraints:
-300 <= x, y <= 3000 <= |x| + |y| <= 300Problem Overview: You start with a knight at (0,0) on an infinite chessboard. The task is to reach a target coordinate (x, y) using the minimum number of knight moves. Each move follows the classic L-shape pattern: two steps in one direction and one step perpendicular.
Approach 1: Breadth-First Search (Shortest Path) (Time: O((|x|+|y|)^2), Space: O((|x|+|y|)^2))
This problem is a shortest path search on an implicit graph. Each board coordinate represents a node, and the 8 possible knight moves form edges. Breadth-First Search works perfectly because every move has equal cost. Start from (0,0), push it into a queue, and explore all valid knight moves level by level. Use a visited set to avoid revisiting coordinates.
The key observation: the board is symmetric around both axes. You can convert the target to the first quadrant using x = abs(x) and y = abs(y). This drastically reduces the search space. During BFS, limit exploration to coordinates slightly beyond the target (commonly >= -2). This prevents the queue from expanding infinitely while still preserving correctness. The first time you dequeue the target coordinate, the level count equals the minimum number of moves.
This approach models the board as an unweighted graph and computes the shortest path using standard BFS traversal. The search region grows roughly proportional to the Manhattan distance from the origin.
Approach 2: Optimized BFS with Symmetry Pruning (Time: O((|x|+|y|)^2), Space: O((|x|+|y|)^2))
The default optimized solution still uses BFS but aggressively prunes the search space using geometric symmetry. Because knight movement is symmetric across both axes and diagonals, convert the target to the first quadrant and only explore positions where x >= -2 and y >= -2. Positions further negative never contribute to the optimal path.
Each BFS step generates the eight possible knight moves and pushes unseen positions into the queue. The pruning rule prevents unnecessary exploration in distant directions. In practice this reduces the state space dramatically, making the algorithm fast even for large coordinates like (300, 300). The algorithm still guarantees correctness because the shortest path never requires moving far away from the target direction.
This technique combines BFS traversal with symmetry reduction, a common optimization for grid and graph search problems.
Recommended for interviews: BFS with symmetry pruning. Interviewers expect you to recognize the problem as an unweighted shortest-path search and apply BFS. Mentioning symmetry (abs(x), abs(y)) and bounding the search to avoid infinite expansion demonstrates deeper algorithmic thinking. A plain BFS shows understanding, but the pruned BFS shows strong problem-solving skills.
This problem can be solved using the BFS shortest path model. The search space for this problem is not large, so we can directly use the naive BFS. The solution below also provides the code for bidirectional BFS for reference.
Bidirectional BFS is a common optimization method for BFS. The main implementation ideas are as follows:
| Approach | Complexity |
|---|---|
| BFS | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search | O((|x|+|y|)^2) | O((|x|+|y|)^2) | General shortest-path solution on the infinite board |
| Optimized BFS with Symmetry Pruning | O((|x|+|y|)^2) | O((|x|+|y|)^2) | Preferred approach in interviews; reduces search space using symmetry |
Leetcode 1197 | Minimum Knight Moves (Solution Explained) | Asked by Mathworks, DoorDash, & Amazon • AverageLeeter • 13,288 views views
Watch 9 more video solutions →Practice Minimum Knight Moves with our built-in code editor and test cases.
Practice on FleetCode