Question
I have been given: a print function, and the functions below. We are to find all knights tours for a 5x5 chessboard. We have to
I have been given: a print function, and the functions below. We are to find all knights tours for a 5x5 chessboard. We have to implement two functions, void KnightsTour::get_moves( int row, int col, int row_moves[], int col_moves[], int& num_moves) and void KnightsTour::move(int row, int col, int& m, int& num_tours). I need help implementing these. The move function has to use recursive backtracking. The following functions were given:
KnightsTour::KnightsTour(int board_size) { this->board_size = board_size; this->board.resize(board_size); for (int i = 0; i < board_size; ++i) { this->board[i].resize(board_size); } }
int KnightsTour::generate(int row, int col) { int m = 0; int num_tours = 0; move(row, col, m, num_tours); return num_tours; }
Also, the header file was given:
#ifndef KNIGHTS_TOUR_H #define KNIGHTS_TOUR_H
#include
class KnightsTour {
public: KnightsTour(int board_size); int generate(int row, int col);
private: void move(int row, int col, int& m, int& num_tours); void get_moves(int row, int col, int row_moves[], int col_moves[], int& num_moves ); void print(); int board_size; vector
};
#endif
The main file was also given:
#include
int main(int argc, char** argv) { if (argc != 3) { cout << "Invalid input" << endl; return 0; }
int row = atoi(argv[1]); int col = atoi(argv[2]); if (row < 0 || row > 4 || col < 0 || col > 4) { cout << "Invalid starting location" << endl; return 0; }
KnightsTour knight(5);
int num_tours = knight.generate(row, col); cout << "Number of tours: " << num_tours << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started