// knight.cpp : Defines the entry point for the console application. // #include #include #include //using std::cout; //using std::endl; //using std::getch; /* Knight Tour */ const int size = 8; void initialization(); void print(); void Knight ( int times, int row, int col ); int K[size][size]; bool check( int row, int col); int move_col[] = {-2, -1, +1, +2, +2, +1, -1, -2}; int move_row[] = {+1, +2, +2, +1, -1, -2, -2, -1}; int count; void initialization() { count = 0; for ( int i=0; i < size; i++) { for ( int j=0; j < size; j++) { K[i][j] = 0; } } } bool check ( int row, int col ) { if ( row > 7 || row < 0 || col > 7 || col < 0 || K[row][col] != 0 ) return false; else return true; } void print () { index++; cout << " Let's go Knight ^^" << endl; cout << " The : "<< index << "th"<< endl; for ( int i = 0; i < size; i++ ) { for ( int j = 0; j < size; j++ ) { cout << setw(3) << K[i][j] ; } cout << endl; } } void Knight ( int times, int row, int col) { int next_row, next_col; K[row][col] = times + 1; //cout << "times = " << times << endl; if ( times >= 63 ) { cout << "-----------------------" << endl; print(); getch(); } else { for ( int i = 0; i < 8; i++ ) { //cout << " row : " << row << endl; //cout << " col : " << col << endl; next_col = col + move_col[i]; next_row = row + move_row[i]; if (check( next_row,next_col )) { Knight (times+1, next_row, next_col); } } } K[row][col] = 0; } void main() { initialization(); Knight (0,0,0); getch(); }