|
| 1 | +//Solved by M Yuvasri, EEE |
| 2 | + |
| 3 | +#include<stdio.h> |
| 4 | +#include<stdlib.h> |
| 5 | +#define chessBoardSize 12 |
| 6 | + |
| 7 | +int chessBoard[chessBoardSize][chessBoardSize] = {0}; |
| 8 | +typedef struct point{ |
| 9 | + int x, y; |
| 10 | +}POINT; |
| 11 | +int count=0; |
| 12 | + |
| 13 | +int nextPosition(int x, int y, POINT* array){ |
| 14 | + int m=0; |
| 15 | + /* finds the next possible points for the current |
| 16 | + position in the chess board: |
| 17 | + like |
| 18 | + _ _ _ _ _ _ |
| 19 | + _ * _ * _ _ |
| 20 | + * _ _ _ * _ |
| 21 | + _ _ P _ _ _ |
| 22 | + * _ _ _ * _ |
| 23 | + _ * _ * _ _ |
| 24 | + |
| 25 | +as above if 'P' is the current (x,y) |
| 26 | +* represents the next possible points and |
| 27 | +also checks it exists within the chess board |
| 28 | + */ |
| 29 | + |
| 30 | + if( (x+2) < chessBoardSize ){ |
| 31 | + if( (y+1) < chessBoardSize ){ |
| 32 | + array[m].x = x+2; |
| 33 | + array[m++].y = y+1; |
| 34 | + } |
| 35 | + if( (y-1) >-1 ){ |
| 36 | + array[m].x = x+2; |
| 37 | + array[m++].y = y-1; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if( (x-2) > -1){ |
| 42 | + if( (y+1) < chessBoardSize ){ |
| 43 | + array[m].x = x-2; |
| 44 | + array[m++].y = y+1; |
| 45 | + } |
| 46 | + if( (y-1) >-1 ){ |
| 47 | + array[m].x = x-2; |
| 48 | + array[m++].y = y-1; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if( (y+2) < chessBoardSize){ |
| 53 | + if( (x+1) < chessBoardSize ){ |
| 54 | + array[m].x = x+1; |
| 55 | + array[m++].y = y+2; |
| 56 | + } |
| 57 | + if( (x-1) >-1 ){ |
| 58 | + array[m].x = x-1; |
| 59 | + array[m++].y = y+2; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if( (y-2) > -1){ |
| 64 | + if( (x+1) < chessBoardSize ){ |
| 65 | + array[m].x = x+1; |
| 66 | + array[m++].y = y-2; |
| 67 | + } |
| 68 | + if( (x-1) >-1 ){ |
| 69 | + array[m].x = x-1; |
| 70 | + array[m++].y = y-2; |
| 71 | + } |
| 72 | + } |
| 73 | + return m; |
| 74 | +} |
| 75 | + |
| 76 | +void displayAnswer(){ |
| 77 | + int i, j, k; |
| 78 | + printf("\n"); |
| 79 | + for(i=0; i<chessBoardSize; i++){ |
| 80 | + for(j=0; j<chessBoardSize; j++) |
| 81 | + printf("%d\t",chessBoard[i][j]); |
| 82 | + printf("\n\n"); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// recursive function using backtrack method |
| 87 | +void knightTravel(int x, int y){ |
| 88 | + POINT array[8] = {{0, 0}, {0, 0}}; |
| 89 | + // remainin initialized to zero automatically |
| 90 | + volatile int noOfPossiblePoints = nextPosition(x, y, array); |
| 91 | + volatile int i; |
| 92 | + |
| 93 | + chessBoard[x][y] = ++count; |
| 94 | + |
| 95 | + // base condition uses count |
| 96 | + if( count == chessBoardSize * chessBoardSize ){ |
| 97 | + displayAnswer(); |
| 98 | + exit(0); |
| 99 | + } |
| 100 | + |
| 101 | + for(i=0; i< noOfPossiblePoints; i++) |
| 102 | + if( chessBoard[array[i].x][array[i].y] == 0 ) |
| 103 | + knightTravel(array[i].x, array[i].y); |
| 104 | + |
| 105 | + chessBoard[x][y] = 0; |
| 106 | + count--; |
| 107 | +} |
| 108 | + |
| 109 | +int main() |
| 110 | +{ |
| 111 | + knightTravel(0, 0); |
| 112 | + printf("No solution exists\n"); |
| 113 | + return 0; |
| 114 | +} |
0 commit comments