Deitel & Deitel, C How to Program 6th Edition, Exercise 6.24 (Knight’s Tour)
//6.24 (Knight’s Tour)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARDSIZE 8
#define STARTINGROW 2 //0 to 7
#define STARTINGCOLUMN 2 //0 to 7
int main() {
int hor[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int ver[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int currentRow, currentColumn, moveNumber, testRow, testColumn;
int board[BOARDSIZE][BOARDSIZE];
int i, j, k, a, b, x, y;
int stack, access;
int acc[12][12] = {
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,2,3,4,4,4,4,3,2,0,0,
0,0,3,4,6,6,6,6,4,3,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,3,4,6,6,6,6,4,3,0,0,
0,0,2,3,4,4,4,4,3,2,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};
int accesibility[12][12];
for (a = 0; a <= 7; a++) {
for (b = 0; b <= 7; b++) {
for (j = 0; j <= 11; j++)
for (k = 0; k <= 11; k++)
accesibility[j][k] = acc[j][k];
for (j = 0; j <= 7; j++)
for (k = 0; k <= 7; k++)
board[j][k] = 0;
accesibility[a + 2][b + 2] = 0;
board[a][b] = 1;
currentRow = a;
currentColumn = b;
stack = -1;
i = 2;
while (1) {
access = 10;
for (moveNumber = 0; moveNumber <= 7; moveNumber++) {
testRow = currentRow + ver[moveNumber];
testColumn = currentColumn + hor[moveNumber];
if (accesibility[testRow + 2][testColumn + 2] != 0 && accesibility[testRow + 2][testColumn + 2] < access) {
access = accesibility[testRow + 2][testColumn + 2];
--accesibility[testRow + 2][testColumn + 2];
stack = moveNumber;
}
}
if (stack == -1)
break;
moveNumber = stack;
currentRow += ver[moveNumber];
currentColumn += hor[moveNumber];
board[currentRow][currentColumn] = i;
accesibility[currentRow + 2][currentColumn + 2] = 0;
stack = -1;
i++;
}
for (x = 2; x < 10; x++) {
for (y = 2; y < 10; y++)
printf("%4d", accesibility[x][y]);
printf("\n");
}
printf("\n");
for (x = 0; x < 8; x++) {
for (y = 0; y < 8; y++)
printf("%4d", board[x][y]);
printf("\n");
}
printf("end of %d >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", 8*a+b+1);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARDSIZE 8
#define STARTINGROW 2 //0 to 7
#define STARTINGCOLUMN 2 //0 to 7
int main() {
int hor[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int ver[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int currentRow, currentColumn, moveNumber, testRow, testColumn;
int board[BOARDSIZE][BOARDSIZE];
int i, j, k, a, b, x, y;
int stack, access;
int acc[12][12] = {
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,2,3,4,4,4,4,3,2,0,0,
0,0,3,4,6,6,6,6,4,3,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,4,6,8,8,8,8,6,4,0,0,
0,0,3,4,6,6,6,6,4,3,0,0,
0,0,2,3,4,4,4,4,3,2,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};
int accesibility[12][12];
for (a = 0; a <= 7; a++) {
for (b = 0; b <= 7; b++) {
for (j = 0; j <= 11; j++)
for (k = 0; k <= 11; k++)
accesibility[j][k] = acc[j][k];
for (j = 0; j <= 7; j++)
for (k = 0; k <= 7; k++)
board[j][k] = 0;
accesibility[a + 2][b + 2] = 0;
board[a][b] = 1;
currentRow = a;
currentColumn = b;
stack = -1;
i = 2;
while (1) {
access = 10;
for (moveNumber = 0; moveNumber <= 7; moveNumber++) {
testRow = currentRow + ver[moveNumber];
testColumn = currentColumn + hor[moveNumber];
if (accesibility[testRow + 2][testColumn + 2] != 0 && accesibility[testRow + 2][testColumn + 2] < access) {
access = accesibility[testRow + 2][testColumn + 2];
--accesibility[testRow + 2][testColumn + 2];
stack = moveNumber;
}
}
if (stack == -1)
break;
moveNumber = stack;
currentRow += ver[moveNumber];
currentColumn += hor[moveNumber];
board[currentRow][currentColumn] = i;
accesibility[currentRow + 2][currentColumn + 2] = 0;
stack = -1;
i++;
}
for (x = 2; x < 10; x++) {
for (y = 2; y < 10; y++)
printf("%4d", accesibility[x][y]);
printf("\n");
}
printf("\n");
for (x = 0; x < 8; x++) {
for (y = 0; y < 8; y++)
printf("%4d", board[x][y]);
printf("\n");
}
printf("end of %d >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", 8*a+b+1);
}
}
return 0;
}
Let me know in the comments if this page helped you somehow.
It is nice to know if my work helped someone, thanks :)
Comments
Post a Comment