Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

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 > board;

};

#endif

The main file was also given:

#include #include #include "knights_tour.h" using namespace std;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

Explain the legal environments impact on labor relations. page 631

Answered: 1 week ago

Question

State the uses of job description.

Answered: 1 week ago

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago