Chess knight move in 8x8 chessboard

5.6k Views Asked by At

I've recently shown some interests in chess, and I wonder if there is a solution for the following problem:

In a 8x8 chessboard, labeling the cells with numbers from 1 to 8, is there any way to find the minimum moves that a knight should do to reach a given destination? For example, in this configuration:

  1  2  3  4  5  6  7  8
1
2
3
4
5
6
7
8

If the start position is the cell $(1, 1)$, and the destination is $(5, 1)$, we can go there with only 2 moves. First at $(3, 2)$, and then at $(5, 1)$. There are no restricts to be noticed, we can suppose that there is a blank 8x8 chessboard with only a night in it. I only want to know that if the start position is $(x_1, y_1)$ and the destination is $(x_2, y_2)$, how many moves are required to go there (the minimum)? Does anyone has experiences on this?

4

There are 4 best solutions below

0
On BEST ANSWER

Following the suggestion of @BarakManos:

Step 1: Construct a graph where each square of the chess board is a vertex.

Step 2: Place an edge between vertices exactly when there is a single knight-move from one square to another.

Step 3: Dijkstra's algorithm is an algorithm to find the length of a path between two vertices (squares). It is very classical in graph theory, data structures, and algorithms.

3
On

There's actually few restrictions that are due to the edge of the board. One of these (or the only case) is moving from A1 to B2 for example. Normally it would take two moves to move one step diagonally, but here that solution would move you of board. Instead you would have to move to C2 or B3 first and then it's a move one step along file or row which is three steps.

Then it should be quite straight forward to just testing for solutions. The reason you don't get that many restrictions from the edges is that there will be similar solutions by symmetry that would avoid the restriction of the edges.

I wrote a program using the following algorithm:

  1. Create a array of squares
  2. Write zero in one array, then for each $j\in\mathbb N$:
  3. For each square with value $j$ put $j+1$ in all empty squares reachable by a knight jump. Repeat for successive $j$'s until the board is filled.

Well this is kind of similar to Dijkstra's algorithm (except in this case the distances are all one). My program confirmed that on a regular chess board the only case where the edges are a restriction is the mentioned case.

1
On

Dijkstra is overkill here.
Construct the graph as said in other answers, since the weight of edges are add one, one can find shortest path in a simple graph using Breadth First Search in O(V+E) instead of O(E + VlogV) as in Dijkstra.
If you want to find all of shortest paths from all cell to all others, you can use Floyd-Warshall algorithm since your vertexes in the graph are 64, this can be computed in O(n^3).
I should add that between Dijkstra, BFS and Floyd-Warshall , Floyd-Warshall is the easiest one to implement . and it is feasible to use it up to a 10*10 board.


In this video you can find out more about finding shortest path using BFS
And in this wiki you can learn about Floyd-Warshall algorithm.

1
On

A little late to be answering, but I created a code and implemented it to see the results:

0 3 2 3 2 3 4 5
3 4 1 2 3 4 3 4 
2 1 4 3 2 3 4 5 
3 2 3 2 3 4 3 4 
2 3 2 3 4 3 4 5 
3 4 3 4 3 4 5 4 
4 3 4 3 4 5 4 5
5 4 5 4 5 4 5 6

The knight is at position 0 on the 8 by 8 board, and takes at most 6 moves to get anywhere (the opposite corner).

The symmetry of a chess board is 8 fold, since it can be reflected along either diagonal to two unique positions, or rotated 90 degrees to four unique positions. This means there are 8 slivers of the board. I decided to post up the 9 other positions that are distinct under this transform, so that stack exchange will have the actual information on this for the future. Under these permutations and symmetries, one can find out what the distance for the knight is to any position on the board.

They are as follows:

Edge positions:

3 0 3 2 3 2 3 4     2 3 0 3 2 3 2 3     3 2 3 0 3 2 3 2
2 3 2 1 2 3 4 3     1 2 3 2 1 2 3 4     2 1 2 3 2 1 2 3
1 2 1 4 3 2 3 4     4 1 2 1 4 3 2 3     3 4 1 2 1 4 3 2
2 3 2 3 2 3 4 3     3 2 3 2 3 2 3 4     2 3 2 3 2 3 2 3 
3 2 3 2 3 4 3 4     2 3 2 3 2 3 4 3     3 2 3 2 3 2 3 4
4 3 4 3 4 3 4 5     3 4 3 4 3 4 3 4     4 3 4 3 4 3 4 3
3 4 3 4 3 4 5 4     4 3 4 3 4 3 4 5     3 4 3 4 3 4 3 4
4 5 4 5 4 5 4 5     5 4 5 4 5 4 5 4     4 5 4 5 4 5 4 5

Next Row:

4 3 2 1 2 3 4 3     1 2 3 2 1 2 3 4     2 1 2 3 2 1 2 3
3 0 3 2 3 2 3 4     2 3 0 3 2 3 2 3     3 2 3 0 3 2 3 2
2 3 2 1 2 3 4 3     1 2 3 2 1 2 3 4     2 1 2 3 2 1 2 3
1 2 1 4 3 2 3 4     4 1 2 1 4 3 2 3     3 4 1 2 1 4 3 2
2 3 2 3 2 3 4 3     3 2 3 2 3 2 3 4     2 3 2 3 2 3 2 3
3 2 3 2 3 4 3 4     2 3 2 3 2 3 4 3     3 2 3 2 3 2 3 4
4 3 4 3 4 3 4 5     3 4 3 4 3 4 3 4     4 3 4 3 4 3 4 3
3 4 3 4 3 4 5 4     4 3 4 3 4 3 4 5     3 4 3 4 3 4 3 4

Center:

4 1 2 1 4 3 2 3     3 4 1 2 1 4 3 2     2 3 2 3 2 3 2 3
1 2 3 2 1 2 3 4     2 1 2 3 2 1 2 3     3 4 1 2 1 4 3 2
2 3 0 3 2 3 2 3     3 2 3 0 3 2 3 2     2 1 2 3 2 1 2 3
1 2 3 2 1 2 3 4     2 1 2 3 2 1 2 3     3 2 3 0 3 2 3 2
4 1 2 1 4 3 2 3     3 4 1 2 1 4 3 2     2 1 2 3 2 1 2 3
3 2 3 2 3 2 3 4     2 3 2 3 2 3 2 3     3 4 1 2 1 4 3 2
2 3 2 3 2 3 4 3     3 2 3 2 3 2 3 4     2 3 2 3 2 3 2 3
3 4 3 4 3 4 3 4     4 3 4 3 4 3 4 3     3 2 3 2 3 2 3 4

Sorry if this isn't very pretty. All you have to do to get the knight into another position is rotate your board or flip the numbers along a diagonal. Note that, as most chess players already know, knights are much more powerful in the center of the board.