Anda di halaman 1dari 4

//Chapter 6 Exercise //6.24 Knight's Tour #include <stdio.h> #include <stdlib.h> #include <windows.

h> //Function prototype //FUNCTION printChessBoard void printChessBoard(int b[][8]); //FUNCTION findMinAccessibility int findMinAccessibility(int b[][8], int a[][8], int v[], int h[], int row, int column); //FUNCTION changeAccessibility void changeAccessibility(int a[][8], int v[], int h[], int row, int column); //Function main begins program execution int main (void) { //Variables declaration & Initialization int board [8][8] = {0}; int horizontal [8] = {2, 1, -1, -2, -2, -1, 1, 2}; int vertical [8] = {-1, -2, -2, -1, 1, 2, 2, 1}; int moveNumber; int currentRow; int currentColumn; int accessibility[8][8] = {{2, 3, 4, 4, 4, 4, 3, 2}, {3, 3}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {3, 3}, {2, 2}}; int counter; int startRow; int startColumn; int testFullTour; int testClosedTour = 0;

4, 6, 6, 6, 6, 4, 6, 8, 8, 8, 8, 6, 6, 8, 8, 8, 8, 6, 6, 8, 8, 8, 8, 6, 6, 8, 8, 8, 8, 6, 4, 6, 6, 6, 6, 4, 3, 4, 4, 4, 4, 3,

puts("THE KNIGHT'S TOUR"); puts(""); puts("Can the chess piece called the knight move around an empty chessbo ard"); puts("and touch each of the 64 squares once and only once?"); puts(""); printf("%s", "Enter starting row number (0 to 7): "); scanf_s("%d", &currentRow); printf("%s", "Enter starting column number (0 to 7): "); scanf_s("%d", &currentColumn); board [currentColumn][currentRow] = 1;

startRow = currentRow; startColumn = currentColumn; printf("Generating chess board"); Sleep(500); printf(" ."); Sleep(500); printf(" ."); Sleep(500); printf(" ."); system("cls"); puts("Move 1 was made."); for(counter = 2; counter <= 64; counter++){ Sleep(100); system("cls"); moveNumber = findMinAccessibility(board, accessibility, vertical , horizontal, currentRow, currentColumn); if(moveNumber != -1){ currentRow += vertical[moveNumber]; currentColumn += horizontal[moveNumber]; board [currentColumn][currentRow] = counter; changeAccessibility(accessibility, vertical, horizontal, currentRow, currentColumn); testFullTour = 1; } else{ testFullTour = 0; } printf("\nCurrent position: Row %d Column %d\n", currentRow, cur rentColumn); printChessBoard(board); } counter = 0; for(int i = 0; i <= 7; i++){ if(currentRow + vertical[i] >= 0 && currentRow + vertical[i] <= 7){ if(currentColumn + horizontal[i] >= 0 && currentColumn + horizontal[i] <= 7){ if(currentRow + vertical[i] == startRow){ if(currentColumn + horizontal [i] == sta rtColumn){ counter++; } } } } } if(counter == 1){ testClosedTour = 1; }

if(testClosedTour == 1 && testFullTour == 1){ puts("Closed Tour!"); } else if(testFullTour == 1){ puts("Full tour!"); } else{ puts("Not a full tour!"); } return 0; }//End function main //User-defined function //FUNCTION printChessBoard void printChessBoard(int b[][8]) { puts(" [0][1][2][3][4][5][6][7]"); for(int row = 0; row <= 7; row++){ printf("[%d] ", row); for(int column = 0; column <= 7; column++){ printf("%2d ", b[column][row]); } puts(""); } } //FUNCTION findMinAccessibility int findMinAccessibility(int b[][8], int a[][8], int v[], int h[], int row, int column) { int min = 8; int move = -1; int counter = 0; int possibleMove[8] = {0}; for(int i = 0; i<=7; i++){ if(row + v[i] >= 0 && row + v[i] <= 7){ if(column + h[i] >= 0 && column + h[i] <= 7){ if(b[column + h[i]][row + v[i]] == 0){ if(a[column + h[i]][row + v[i]] <= min){ min = a[column + h[i]][row + v[i ]]; move = i; } } } } } return move; } //FUNCTION changeAccessibility void changeAccessibility(int a[][8], int v[], int h[], int row, int column) { for(int i = 0; i <= 7; i++){ if(row + v[i] >= 0 && row + v[i] <= 7){ if(column + h[i] >= 0 && column + h[i] <= 7){ a[column + h[i]][row + v[i]]--;

} } } }