User:Benhoman/Sandbox



Warnsdorff's algorithm is a heuristic method for solving the Knight's Tour. The algorithm was first described in "Des Rösselsprungs einfachste und allgemeinste Lösung" by H. C. Warnsdorff in 1823.

Overview
Warnsdorff's rule was to always choose the next square that had the fewest possible further knight's moves. This more generally can be applied to any graph, by always choosing the next node that has smallest degree. Pohl generalized this by adding a rule for breaking ties. Although the Hamiltonian path problem is NP-hard in general, on many graphs that occur in practice this linear-time heuristic is able to successfully locate a solution. The knight's tour is a special case.

How it Works
Warnsdorff’s algorithm for solving the Knight’s Tour problem works by placing the knight at any position on the board. The algorithm will then assess find the next possible moves it can take. From there the algorithm then calculates how many possible moves each of those next moves can make. The algorithm then moves to the one that has the least amount of next possible moves and repeats the process again.

What is the Knight’s Tour
The Knight’s Tour is an ancient puzzle, where the goal is to move a knight to every position on a chess board without going to the same place twice. The Knight's Tour is a common problem in Computer Science classes that students must solve by creating an algorithm in a programming language. Warnsdorff's algorithm is only one option. An example of another answer to this problem would be a brute force attack algorithm, where the program will go from move to move until it either finds a working tour or it can't move anymore in which case the program will backtrack and tries a different path.

Example of the Knight’s Tour Problem Solved in C++
The following code is an example of the source from a C++ file that will take an eight by eight chess board and solve the Knight’s Tour problem starting from a random position on the chess board.


 * 1) include 
 * 2) include 
 * 3) include 

int checkMovability( int[][ 8 ], int, int ); void printBoard( int[][ 8 ] );

int main {   int currentRow = rand % 8, currentColumn = rand % 8, moveNumber, counter = 1; int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 }, horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int board[ 8 ][ 8 ] = { 0 }; int tourLength[ 65 ] = { 0 }; int tour, i;   srand( time(NULL) ); for (tour = 1; tour <= 1000; ++tour) { while ( checkMovability( board, currentRow, currentColumn ) == 1 && counter <= 64 ) { moveNumber = rand % 8; if ( board[ currentRow + vertical[ moveNumber ] ][ currentColumn + horizontal[ moveNumber ] ] == 0 ) { //        printf("inner if 1\n"); if ( ( currentRow + vertical[ moveNumber ] ) <= 7 && ( currentRow + vertical[ moveNumber ] ) >= 0 ) { //           printf("inner if 2\n"); if ( (currentColumn + horizontal[ moveNumber ]) <= 7 && ( currentColumn + horizontal[ moveNumber ] ) >= 0 ) { currentRow += vertical[ moveNumber ]; currentColumn += horizontal[ moveNumber ]; board[ currentRow ][ currentColumn ] = counter; ++counter; //              printBoard( board ); //              printf("Knight at %d %d\n", currentRow, currentColumn); }               }            }        }        //   printBoard( board ); //  printf("No more moves can be made\n"); ++tourLength[ counter - 1]; }   for (i = 0; i <= 64; ++i) { printf("%d: %d\n", i, tourLength[ i ] ); }   return 0; }

int checkMovability( int grid[][ 8 ], int row, int column ) {   int h[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int v[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int move; for (move = 0; move <= 7; ++move) { if ( grid[ row + v[ move ] ][ column + h[ move ] ] == 0 ) { if ( ( row + v[ move ] ) <= 7 && ( row + v[ move ] ) >= 0 ) { if ( (column + h[ move ]) <= 7 && ( column + h[ move ] ) >= 0 ) { //              printf("checkMovability returned 1\n"); //              printf("checkMovability says you can move v: %d h: %d\n", v[ move ], h[ move ] ); //              printBoard( grid ); return 1; }           }        }    }    return 0; }

void printBoard( int grid[][ 8 ] ) {   int i, j;    printf("\n\n"); for (i = 0; i <= 7; i++) { for (j = 0; j <= 7; j++) { printf("%4d", grid[ i ][ j ] ); if ( (j + 1) % 8 == 0 ) printf("\n\n"); }   }    return; }