recursion - Recursive backtracking knights tour in c ++ -
so, in short i'm working on knight's tour program. if don't know is, knight gets put on chess board , have move every spot on board once. i'm using recursive function having trouble getting backtracking work. can 22 steps in 5x5 board program won't , try different path. i'm posting recursive part of code (sorry it's little long) insight extremely helpful. lot!
`bool findpath ( int board[][boardsize + 4], int &currrow, int &currcol, int &currmove,int boardsize ) { int i, j; bool foundspot; board[currrow][currcol] = currmove; if ( currmove == boardsize * boardsize ) return true; ( = 0; < boardsize + 4; i++ ) { ( j = 0; j < boardsize + 4; j++ ) cout << setw (3) << board[i][j]; cout<<endl; } cout << endl; if ( board[currrow - 2][currcol - 1] == 0 ) { currmove += 1; board[currrow - 2][currcol - 1] = currmove; currrow -= 2; currcol -= 1; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow - 2][currcol + 1] == 0 ) { currmove += 1; board[currrow - 2][currcol + 1] = currmove ; currrow -= 2; currcol += 1; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow - 1][currcol + 2] == 0 ) { currmove += 1; board[currrow - 1][currcol + 2] = currmove ; currrow -= 1; currcol += 2; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow + 1][currcol + 2] == 0 ) { currmove += 1; board[currrow + 1][currcol + 2] = currmove ; currrow += 1; currcol += 2; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow + 2][currcol + 1] == 0 ) { currmove += 1; board[currrow + 2][currcol + 1] = currmove ; currrow += 2; currcol += 1; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow + 2][currcol - 1] == 0 ) { currmove += 1; board[currrow + 2][currcol - 1] = currmove ; currrow += 2; currcol -= 1; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow + 1][currcol - 2] == 0 ) { currmove += 1; board[currrow + 1][currcol - 2] = currmove ; currrow += 1; currcol -= 2; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } if ( board[currrow - 1][currcol - 2] == 0 ) { currmove += 1; board[currrow - 1][currcol - 2] = currmove ; currrow -= 1; currcol -= 2; if ( findpath( board, currrow, currcol, currmove, boardsize ) ) return true; } board[currrow][currcol] = 0; currmove -= 2; return false; }`
i worked out following implementation of knights tour in c++ :
#include<cstdio> #include<iostream> #define max 10 using namespace std; int tour=1; int board[max][max]; bool is_travelled(int,int); bool knights_tour(int,int,int,int); void initialize(int); void display(int); int main(int argc,char** argv){ int n; scanf("%d",&n); for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ board[i][j]=-1; } } initialize(n); display(n); return 0; } bool is_travelled(int x,int y){ if(x<0 || y<0)return true; if(board[x][y]==-1)return false; return true; } bool knights_tour(int i,int j,int n,int k){ // k=number of places remained , n=side of chess_board; int x,y; if(k==0)return true; // hard-coded cases; // reordering of cases have significant effect on execution time x=i+2;y=j+1; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i+1;y=j+2; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i-1;y=j+2; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i-2;y=j+1; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i-2;y=j-1; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i-1;y=j-2; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i+1;y=j-2; if((!is_travelled(x,y))&&x+y<n&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } x=i+2;y=j-1; if((!is_travelled(x,y))&&x<n&&y<n){ board[x][y]=tour; tour+=1; if(knights_tour(x,y,n,k-1))return true; board[x][y]=-1; tour-=1; } return false; } void initialize(int n){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ board[i][j]=0; int r=knights_tour(i,j,n,n*n-1); if(r==1)return; board[i][j]=-1; } } } void display(int n){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%2d ",board[i][j]); } printf("\n"); } cout<<endl; }
hope helps . happy coding !
Comments
Post a Comment