Knight Tour Loops Problem

I'm trying to solve the problem for knight tour which each square only allow visited 1 time.



#include<iostream>
using namespace std;

void printBoard(int[][8]);
bool validMove(int, int, const int[][8]);
bool knightTour(int[][8], int , int , int, int, int[], int[]);

int main()
{
int count=0;
int board[8][8] = {0};
int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int currentRow, currentColumn, beforeRow, beforeColumn;
int moveNumber;

cout<<"Enter the current row for Knight: ";
cin>> currentRow;
cout<<"Enter the current column for Knight:";
cin>> currentColumn;

if(knightTour(board, currentRow, currentColumn, count, moveNumber, horizontal, vertical)==true)
printBoard(board);
else
cout<<"No solution"<<endl;

system("PAUSE");
}

void printBoard (int chess[][8])
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
cout<< chess[i][j];
cout<<endl;
}
}

bool validMove(int row, int column, const int chess[][8])
{
if((row>=0 && row<8) && (column>=0 && column<8) && (chess[row][column]==0))
{
return true;
}
return false;
}



//m= moveNumber, v=vertical, h=horizontal, c=count
bool knightTour(int chess[][8], int row, int column, int c, int m, int h[], int v[])
{
if(c==63)
return true;

for(m=0; m<8; m++)
{
row = row + h[m];
column = column + v[m];
if(validMove(row, column, chess)==true)
{
chess[row][column] = c+1;
if ((chess[row][column]) == c+1)
return true;
else
chess[row][column] = 0;
}
}
return false;


}

input currentRow =4 , currentColumn =3
output:
00000000
00000000
00000000
00000000
00000000
00000000
00100000
00000000

What wrong with my program?
Last edited on
1. Your code is heavily unreadable. Please use [code ]/*Your Code Here*/[/code] tags which you easily may put into your chat using this funny "<>"-button on the right of this message composing window.

2. What do you expect your program hast to do and what is it doing instead?
Topic archived. No new replies allowed.