TITLE: minimum number of steps for knight in chess
QUESTION [14 upvotes]: Given two squares on an 8×8 chess board, how can we determine the minimum number of moves required by a knight to reach one square starting from the other?

REPLY [17 votes]: If you were considering an infinite chess board, you might get a solution as a (slightly complicated) formula, but on a finite board the restriction of edges (and especially corners) does affect things. Obviously you can draw maps:

And there you can see that the corner position of the knight means that it is quite slow to reach its diagonally neighbouring square, whereas with an open-board knight: 

diagonally-adjacent squares are reached more quickly.

Additional thought:
Outside the $5\times 5$ square centred on the knight, the move-distance pattern becomes simpler. Then if you find $\Delta x, \Delta y$ (unsigned), compute the maximum of $\left ( \frac{\Delta x}{2},\frac{\Delta y}{2},\frac{\Delta x+\Delta y}{3} \right )$ and round up to the nearest integer. Call this $m'$. Now calculate the move count $m$ as follows:
$$
m=m'+((m'+\Delta x+\Delta y) \bmod 2)
$$
To handle close-in squares (on a board of at least $5\times 5$) we can list the exceptions:
$$ \begin{align}
\Delta x =\Delta y =2 &\implies m=4 \\
\Delta x+\Delta y =1 &\implies m=3 \\
\text{For a knight in a corner only, }\Delta x=\Delta y =1 &\implies m=4 \hspace{3in}
\end{align}$$