/* This function solves the Knight Tour problem using Backtracking. This function mainly uses solveKTUtil() to solve the problem. It returns false if no complete tour is possible, otherwise return true and prints the tour. Please note that there may be more than one solutions, this function prints one of the feasible solutions. */ bool solveKT() { int sol[N][N]; /* Initialization of solution matrix */ for (int x = 0; x < N; x++) for (int y = 0; y < N; y++) sol[x][y] = -1; /* xMove[] and yMove[] define next move of Knight. xMove[] is for next value of x coordinate yMove[] is for next value of y coordinate */ int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; // Since the Knight is initially at the first block sol[0][0] = 0; /* Start from 0,0 and explore all tours using solveKTUtil() */ if(solveKTUtil(0, 0, 1, sol, xMove, yMove) == false) { printf("Solution does not exist"); return false; } else printSolution(sol); return true; }
int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[N], int yMove[N]) { int k, next_x, next_y; if (movei == N*N) return 1; /* Try all next moves from the current coordinate x, y */ for (k = 0; k < 8; k++) { next_x = x + xMove[k]; next_y = y + yMove[k]; printf("\nxnext = %d\nynext = %d\nmove = %d\nk = %d", next_x, next_y, movei, k); if (isSafe(next_x, next_y, sol)) { sol[next_x][next_y] = movei; if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove) == 1) return 1; else sol[next_x][next_y] = -1;// backtracking } } return 0; }
void solveKT(){ int sol[N][N]; int i,j; for(i=0;i<N;i++){ for(j=0;j<N;j++){ sol[i][j]=-1; } } int x_move[N]={2,1,-1,-2,-2,-1,1,2}; int y_move[N]={1,2,2,1,-1,-2,-2,-1}; sol[0][0]=0; if(solveKTUtil(0,0,1,sol,x_move,y_move)== false){ printf("Solution not Possible\n"); } else { print_sol(sol); } }
bool solveKTUtil(int x,int y,int movei,int sol[N][N],int x_move[], int y_move[]){ if(movei==N*N) { return true; } int next_x,next_y; int i; for(i=0;i<N;i++){ next_x=x+x_move[i]; next_y=y+y_move[i]; if(is_safe(next_x,next_y,sol)){ sol[next_x][next_y]=movei; if(solveKTUtil(next_x,next_y,movei+1,sol,x_move,y_move)== true){ return true; }else sol[next_x][next_y]=-1; } } return false; }
void KnightsTour() { int chess[N][N]; int x, y; for(x = 0; x < N; x++) for(y = 0; y < N; y++) chess[x][y] = -1; chess[0][0] = 0; //starting position int xmove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int ymove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; printf("\n has found a tour = %d\n", solveKTUtil(0, 0, 1, chess, xmove, ymove)); for(x = 0; x<N; x++) for(y = 0; y<N; y++) if(chess[x][y] >= 0) printf(" (%d, %d) -> ", x, y); printf("\n"); printSolution(chess); }
/* A recursive utility function to solve Knight Tour problem */ int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[N], int yMove[N]) { int k, next_x, next_y; if (movei == N*N) return true; /* Try all next moves from the current coordinate x, y */ for (k = 0; k < 8; k++) { next_x = x + xMove[k]; next_y = y + yMove[k]; if (isSafe(next_x, next_y, sol)) { sol[next_x][next_y] = movei; if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove) == true) return true; else sol[next_x][next_y] = -1;// backtracking } } return false; }