Mar 17, 2013 at 2:03am UTC
#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();
}
OUTPUT:
Enter Length and Height:8 8
Enter X = 1 - 8: 1
Enter Y = 1 - 8: 6
X: 8 Y: 8
X Coor: 1 Y Coor: 6
---------------------------------------------------
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
................
KT: 0 5 1
KT: 1 7 2
KT: 3 6 3
KT: 2 4 4
KT: 1 2 5
KT: 0 0 6
KT: 2 1 7
KT: 0 2 8
KT: 1 4 9
KT: 0 6 10
KT: 2 7 11
KT: 1 5 12
KT: 0 3 13
KT: 2 2 14
KT: 1 0 15
KT: 3 1 16
KT: 2 3 17
KT: 1 1 18
KT: 3 2 19
KT: 2 0 20
KT: 0 1 21
KT: 1 3 22
KT: 2 5 23
KT: 0 4 24
KT: 1 6 25
KT: 3 7 26
KT: 5 6 27
KT: 4 4 28
KT: 6 5 29
KT: 5 3 30
KT: 4 1 31
KT: 3 3 32
KT: 4 5 33
KT: 2 6 34
KT: 0 7 35
KT: 4 7 35
KT: 3 5 36
KT: 5 4 37
KT: 4 2 38
KT: 3 0 39
KT: 5 1 40
KT: 4 3 41
KT: 5 5 42
KT: 3 4 43
KT: 4 6 44
KT: 6 7 45
KT: 7 5 46
KT: 6 3 47
KT: 7 1 48
KT: 5 0 49
KT: 6 2 50
KT: 7 4 51
KT: 6 6 52
KT: 7 0 51
KT: 5 2 49
KT: 4 0 50
KT: 6 1 51
KT: 7 3 52
KT: 6 4 50
KT: 7 6 51
KT: 5 7 52
KT: 7 2 51
KT: 6 0 52
KT: 7 7 30
NOT VALID::
6 21 8 13 24 1 10 35
15 18 5 22 9 12 25 2
20 7 14 17 4 23 34 11
39 16 19 32 43 36 3 26
50 31 38 41 28 33 44 35
49 40 49 30 37 42 27 52
52 51 50 47 50 29 52 45
51 48 51 52 51 46 51 30
.....
Can you help me. My program Knight's Tour is finished. But there's something wrong, Look at the 51 it shows again, When it starts in 1 to 50, then the next number 51 copies, show again! It doesn't finish in 64. Aw. Can you all help me.
Last edited on Mar 17, 2013 at 2:09am UTC