This program is a little complicated and needs deeper understanding. l try to put comments to explain what is going on, if you find something confusing, please leave a comment.
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
template <typename T>
class matrix
{
public:
matrix(int numRows = 1, int numCols = 1, const T& initVal = T());
// constructor.
// Postcondition: create array having numRows x numCols elements
// all of whose elements have value initVal
vector<T>& operator[] (int i);
// index operator.
// Precondition: 0 <= i < nRows. a violation of this
// precondition throws the indexRangeError exception.
// Postcondition: if the operator is used on the left-hand
// side of an assignment statement, an element of row i
// is changed
const vector<T>& operator[](int i) const;
// version for constant objects
int rows() const;
// return number of rows
int cols() const;
// return number of columns
void resize(int numRows, int numCols);
// modify the matrix size.
// Postcondition: the matrix has size numRows x numCols.
// any new elements are filled with the default value of type T
private:
int nRows, nCols;
// number of rows and columns
vector<vector<T> > mat;
// matrix is implemented as nRows vectors (rows),
// each having nCols elements (columns)
};
template <typename T>
matrix<T>::matrix(int numRows, int numCols, const T& initVal):
nRows(numRows), nCols(numCols),
mat(numRows, vector<T>(numCols,initVal))
{}
// non-constant version. provides general access to matrix
// elements
template <typename T>
vector<T>& matrix<T>::operator[] (int i)
{
if (i < 0 || i >= nRows)
cout<<“Matix:Invalid row index”;
return mat[i];
}
// constant version. can be used with a constant object.
// does not allow modification of a matrix element
template <typename T>
const vector<T>& matrix<T>::operator[] (int i) const
{
if (i < 0 || i >= nRows)
cout<<“matrix:invalid row index”;
return mat[i];
}
template <typename T>
int matrix<T>::rows() const
{
return nRows;
}
template <typename T>
int matrix<T>::cols() const
{
return nCols;
}
template <typename T>
void matrix<T>::resize(int numRows, int numCols)
{
int i;
// handle case of no size change with a return
if (numRows == nRows && numCols == nCols)
return;
// assign the new matrix size
nRows = numRows;
nCols = numCols;
// resize to nRows rows
mat.resize(nRows);
// resize each row to have nCols columns
for (i=0; i < nRows; i++)
mat[i].resize(nCols);
}
void print(matrix<int> b,int numofrow)
{
for( int r = 0; r < numofrow ;r++)
{
cout<<endl;
for (int c = 0; c < numofrow ;c++)
{
cout<<setw(4)<<” “<<b[r];
}
cout<<endl;
}
}
bool knight(matrix<int>&board,int k,int i,int j,long &numfxn)
{
numfxn++;//number of function calls
bool result = false;
int u=0,v=0,m = 0;
const int n = board.rows();
static int dx[] = {2,1,-1,-2,-2,-1,1,2}, dy[] ={1,2,2,1,-1,-2,-2,-1};
if (k == n * n)
result = true;
else
{
while(!result)
{
do
{
u = i + dx[m];
v = j + dy[m];
m++;
}while((u < 0 || u > n-1 || v < 0 || v > n-1) && m<7);
if((u >= 0 && u <= n-1) && (v >= 0 && v <= n-1))
{
if (board[u][v] == 0)
{
board[u][v]= k;
k++;
result = knight(board,k,i,j,counter,val,r,c,mpos,escape,numfxn);
}
else
{
//wat to do if a move is right but there is a value;
}
result = knight(board,k,i,j,numfxn);
}
}
}
return result;
}
void main()
{
long numofxncalls = 0;
int size = 0,startrow = -1,startcol = -1,num = 1,response = 0;
while(size < 1)
{
cout<<“Enter a positive integer greater as the Size of the chessboard:”;
cin>>size;
}
board.resize(size,size);
while(startrow < 0|| startrow > size || startcol < 0 || startcol > size)
{
cout<<endl<<endl;
cout<<“Provide the initial location of the Knight within the chessboard”<<endl;
cout<<“Enter Row number:”;
cin>>startrow;
cout<<“Enter Column number:”;
cin>>startcol;
}
startrow–;
startcol–;
board[startrow][startcol] = num;
num++;
if(knight(board,num,startrow,startcol,numofxncalls))
{
cout<<“Number of function calls =”<<numofxncalls<<endl;
cout<<“Below is the complete tour for the knight starting from your initial position”<<endl;
print(board,size);
do
{
cout<<“Press 1 to exit”;
cin>>response;
}while(response != 1);
exit(0);
}
else
{
cout<<“No complete tour Exists”;
do
{
cout<<“Press 1 to exit”;
cin>>response;
}while(response != 1);
exit(0);
}
}