Minimum number of steps for a knight on chess board (5 x 5 or under)

189 Views Asked by At

Given two squares on a chess board of 5x5 or under, how can we determine the minimum number of moves required by a knight to reach one square starting from the other, including a way to determine if it is impossible to reach the knight?

1

There are 1 best solutions below

0
On

Model your knight's errand as a graph: the squares are vertices, edges connect squares if a knight can jump from one to the other. Finding the shortest sequence from one to the other can then be done using e.g. Dijkstra's algorithm.