Knight's Tour. Can you all help me how to explain the whole program? Where having our defense.

Where having our defense on monday. My prof said. I should explain this step by step. Knight's tour. Just like what are the functions, declarations, pointers,....... etc. Can you all help me how to explain this step by step. Complete Program Explanation. Help please.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>

void BoardI(int **board, int X, int Y);
void BoardD(int **board, int X, int Y);
bool KT(int **board, int Coor1, int Coor2, int X, int Y, int Kmove);
bool ValidMove(int **board, int Coor1, int Coor2, int X, int Y);

int main()
{
int X, Y, Coor1, Coor2;
int **board;

do
{
printf("Enter Length and Height:");
scanf("%d %d",&X,&Y);
} while ((X < 1) || (Y < 1));

do
{
printf("Enter X = 1 - %d: ",X);
scanf("%d",&Coor1);
} while ((Coor1 > X) || (Coor1 < 1));

do
{
printf("Enter Y = 1 - %d: ",Y);
scanf("%d",&Coor2);
} while ((Coor2 > Y) || (Coor2 < 1));
printf("X: %d Y: %d\n",X,Y);
printf("X Coor: %d Y Coor: %d\n",Coor1,Coor2);
Coor2--;
Coor1--;

printf("---------------------------------------------------\n");


board = new int *[X];
int **temp = board;

for (int a = 0; a < X; a++)
{
*temp = new int[Y];
(temp)++;
}


BoardI(board, X, Y);
BoardD(board, X, Y);


printf("................");

if (KT(board, Coor1, Coor2, X, Y, 1))
{
printf("VALID:");
BoardD(board, X, Y);
}
else
{
printf("NOT VALID::\n");
BoardD(board,X,Y);
}


for(int i = 0; i < X; i++)
{
delete [] (*board);
(board)++;
}

printf(".....");
scanf("%*1000[^\n]%*1[\n]%*c");


getch();
return 0;
}


void BoardI(int **board, int X, int Y)
{
int x, y;

for (x = 0; x < X; x++)
{
for (y = 0; y < Y; y++)
{
board[x][y] = 0;
}
}
}


void BoardD(int **board, int X, int Y)
{
int x, y;

for (x = 0; x < X; x++)
{
for (y = 0; y < Y; y++)
{
printf("%3d",board[x][y]);
}
printf("\n");
}
}



bool ValidMove(int **board, int Coor1, int Coor2, int X, int Y)
{
if ((Coor1 < 0) || (Coor1 >= X))
{
return false;
}

if ((Coor2 < 0) || (Coor2 >= Y))
{
return false;
}

if (board[Coor1][Coor2] != 0)
{
return false;
}

return true;
}

bool KT(int **board, int Coor1, int Coor2, int X, int Y, int Kmove)
{
if (!ValidMove(board, Coor1, Coor2, X, Y))
{
return false;
}

board[Coor1][Coor2] = Kmove;

if ((X * Y) == Kmove)
{
return true;
}

printf("\nKT: %d %d %d \n",Coor1,Coor2,Kmove);
if (!KT(board, (Coor1 - 1), (Coor2 - 2), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 - 2), (Coor2 - 1), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 - 2), (Coor2 + 1), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 - 1), (Coor2 + 2), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 + 1), (Coor2 + 2), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 + 2), (Coor2 + 1), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 + 2), (Coor2 - 1), X, Y, (Kmove + 1)))
if (!KT(board, (Coor1 + 1), (Coor2 - 2), X, Y, (Kmove + 1)))

return false;
getch();
}

Last edited on
Topic archived. No new replies allowed.