SIgh. Now i took Zahid's advice and tried to re-write the program using recursive functions, which i'm not good at. Here is my code and it has compile errors and i bet it needs still need debugging even if it compiles successfully. Pls help out, before i learn how to do stuff myself i need somebody to give me examples. Pls help.
Thnx in advance
Code:
/* Knight's Tour ( recursive version ) */
#include <stdio.h>
/* 'col' = column */
int knights_tour( int startingRow, int startingCol, int moveNumber ); /* returns the number of moves made */
int out_of_bounds( int nextRow, int nextCol ); /* check if next move is off the board */
int visited( int nextRow, int nextCol, int board[][ 8 ] ); /* check if next square is visited already */
int main()
{
printf( "***Knight's Tour***" );
knights_tour( 0, 0, 0 );
return 0;
)
int out_of_bounds( int nextRow, int nextCol )
{
/* Returns 1 if out of bounds.
Returns 0 if O.K. */
if ( nextRow < 0 || nextRow > 7 )
return 1;
if ( nextCol < 0 || nextCol > 7 )
return 1;
/* O.K. */
return 0;
}
int visited( int nextRow, int nextCol, int board[][ 8 ] )
{
/* Returns 1 if visited.
Returns 0 if not visited. */
if ( board[ nextRow ][ nextCol ] == 1 )
return 1;
return 0;
}
int knights_tour( int startingRow, int startingCol, int moveNumber )
{
int board[ 8 ][ 8 ] = { 0 };
/* moveNumbers: 0 1 2 3 4 5 6 7 */
int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int temp1, temp2;
int currentRow, currentCol;
int totalMoves = 0;
if ( moveNumber > 7 )
return 0;
temp1 = currentRow + vertical[ moveNumber ];
temp2 = currentCol + horizontal[ moveNumber ];
if ( out_of_bounds( temp1, temp2 ) != 1 || visited( temp1, temp2, board ) != 1 ) {
board[ currentRow ][ currentCol ] = 1;
currentRow += vertical[ moveNumber ];
currentCol += horizontal[ moveNumber ];
++totalMoves;
return knights_tour( currentRow, currentCol, moveNumber + 1 ) + totalMoves;
}
}