Breadth-First Search
Medium
Minimum Knight Moves
Find simple shortest path for a Knight on a chessboard.
20 min
Solve on LeetCodeProblem Understanding
You have an infinite chessboard. A Knight starts at coordinates (0, 0).
- Goal: Return the minimum number of steps to move the Knight to square
(x, y). - Moves: The standard 'L' shape:
(±1, ±2)or(±2, ±1).
Algorithm Strategy
This is a Shortest Path on an Unweighted Graph problem.
- Nodes: Coordinates
(row, col). - Edges: The 8 possible Knight jumps.
Since edges are unweighted (cost = 1 move), BFS is the optimal choice.
Optimizations:
- Symmetry: Moving to
(x, y)takes the same steps as(-x, -y),(x, -y), etc. We can useabs(x), abs(y)and restrict our search primarily to the first quadrant (+2 buffer). - Bi-directional BFS: (Advanced) Expand from Start and Target efficiently to meet in the middle.
Interactive Visualization
Step 1 / 1
Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution Code
- Complexity Analysis
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
