6
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ It might be easier to start with an infinite board and to ask the number of moves $M(i,j)$, i.e., the number of moves to move $i$ positions to the right and $j$ positions up. There should be a dynamic programming approach to this problem. $\endgroup$ – Michael Burr Dec 22 '15 at 13:38
  • 5
    $\begingroup$ This sounds like a classic problem for Dijkstra's algorithm. $\endgroup$ – barak manos Dec 22 '15 at 13:39
  • 2
    $\begingroup$ The following paper has a chart on the the second page showing the number of knight-moves required to reach squares. The other approaches portion is very interesting (but requires some advanced mathematics) $\endgroup$ – Michael Burr Dec 22 '15 at 13:51
  • $\begingroup$ codegolf.stackexchange.com/questions/2645/… $\endgroup$ – theREALyumdub Jan 6 '16 at 23:03
7
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
3
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Perhaps you meant four steps in the last line (you can't get to a square after two and three moves due to parity) $\endgroup$ – Michael Burr Dec 22 '15 at 13:49
  • $\begingroup$ I thought it was obvious but ok. Thanks for your suggestion. $\endgroup$ – Dionis Beqiraj Dec 22 '15 at 13:50
  • 1
    $\begingroup$ You have one move to C2 or B3, then from that you have three steps to get to B2 (fx E3, B4, B2). $\endgroup$ – skyking Dec 22 '15 at 13:50
2
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ If any one wanted a code that implements that, tell me to code one , and add it for you. $\endgroup$ – FazeL Dec 22 '15 at 19:50
1
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ This is excellent for my visualization. $\endgroup$ – Dois Jun 1 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.