Breadth-First Search
Medium

Minimum Knight Moves

Find simple shortest path for a Knight on a chessboard.
Problem 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:

  1. Symmetry: Moving to (x, y) takes the same steps as (-x, -y), (x, -y), etc. We can use abs(x), abs(y) and restrict our search primarily to the first quadrant (+2 buffer).
  2. 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.
Unlock VisualizerPREMIUM FEATURE

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.

Start Your Premium Prep