| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Knight's Tour problem
OK...new to this forum, but in need of a little help. I am working on the Night's Tour problem using a recursive, backtracking solution, but my program will never finish. It just locks up the shell evertime, even with a known good solution. It's due tomorrow and I'm a not sure where to go from here:
Code:
#include<iostream>
using namespace std;
class Knight
{
public:
bool valid_pos (int row, int col, int size);
void display_board(int size);
void recursive (int row, int col, int size, int count);
int check_board(int size);
};
//Global vars
const int size=8;//Board size
int board[size][size]; //Create board
int main()
{
Knight check;
int row, col, count=0;
for(int a=0;a<=size;a++) //clear board to 0
{
for(int b=0;b<=size;b++)
{
board[a][b]=0;
}
}
//Take input
cout<<"Enter the start row of the knight: ";
cin>>row;
cout<<"Enter the starting column of the knight: ";
cin>>col;
check.valid_pos (row, col, size);
//Output results
check.recursive(row, col, size, count);
check.display_board(size);
return 0;
}
bool Knight::valid_pos (int row, int col, int size){
if(row>=size || col>=size || row<0 || col<0){//Test for chosen location being within board dimensions
return 1;
}
else{//Check to see if location was not previously visited
if((board[row][col]) == 0){
return 0;
}
else{//returns 1 if location previously visited
return 1;
}
}
}
void Knight::display_board(int size)
{
for(int c=0;c<size;c++)
{
for(int d=0;d<size;d++)
{
if(d==size-1)
{
cout<<"| "<<board[c][d]<<" |"<<endl;
}
else
{
cout<<"| "<<board[c][d]<<" ";
}
}
}
}
int Knight::check_board(int size)
{
int rows, cols, checker;
for(rows=0;rows<size;rows++)
{
for(cols=0;cols<size;cols++)
{
if(valid_pos(rows,cols,size)!=1)
{
checker=0;
}
}
}
return(checker);
}
void Knight::recursive(int row, int col, int size, int count)
{
int checked;
board[row][col]=count;
if(valid_pos (row+2, col+1, size)!=1){
count++;
recursive(row+2, col+1, size, count);
checked=check_board(size);
if(checked==0){
board[row+2][col+1]=0;
count--;
}
}
if(valid_pos (row+2, col-1, size)!=1){
count++;
recursive(row+2, col-1, size, count);
checked=check_board(size);
if(checked==0){
board[row+2][col-1]=0;
count--;
}
}
if(valid_pos (row+1, col+2, size)!=1){
count++;
recursive(row+1, col+2, size, count);
checked=check_board(size);
if(checked==0){
board[row+1][col+2]=0;
count--;
}
}
if(valid_pos (row+1, col-2, size)!=1){
count++;
recursive(row+1, col-2, size, count);
checked=check_board(size);
if(checked==0){
board[row+1][col-2]=0;
count--;
}
}
if(valid_pos(row-2, col+1, size)!=1){
count++;
recursive(row-2, col+1, size, count);
checked=check_board(size);
if(checked==0){
board[row-2][col+1]=0;
count--;
}
}
if(valid_pos (row-2, col-1, size)!=1){
count++;
recursive(row-2, col-1, size, count);
checked=check_board(size);
if(checked==0){
board[row-2][col-1]=0;
count--;
}
}
if(valid_pos (row-1, col+2, size)!=1){
count++;
recursive(row-1, col+2, size, count);
checked=check_board(size);
if(checked==0){
board[row-1][col+2]=0;
count--;
}
}
if(valid_pos (row-1, col-2, size)!=1){
count++;
recursive(row-1, col-2, size, count);
checked=check_board(size);
if(checked==0){
board[row-1][col-2]=0;
count--;
}
}
checked=check_board(size);
if(checked==0){
board[row][col]=0;
count--;
}
}
Last edited by B-Con : September 26th, 2005 at 06:31 PM. Reason: don't forget your [code] tags ;) |
|
#2
|
|||
|
|||
|
I don't think anybody can help you at this short notice.
It also should be handy if you explained the program some more, and when you'd posted it in CODE (making it a lot more readable)... A few remarks: In check_board() the variable checker is not initialized. This means it can have every possible value, but almost never the one you want. I guess you want it to start with 1. This function can also be improved by returning 0 when a empty space is found, this way you can skip the rest of the grid. For the rest, your program looks fine to me.... isn't it just that the program takes a LONG time to determine the solution? I don't see any deadlocks, but who am I ![]() |
|
#3
|
|||
|
|||
|
I just compiled and ran it...
With a size of 4x4, it takes almost no time to finish. With a size of 5x5, it took 14.5s, 5.6s, 2.1s, 3.6s, etc. With a size of 6x6, it took 31s (with solution), 31.2s (with solution), 520.7s=8.5min(with solution!!!!) Some times a solution is found, and sometimes not (seems to be correct). In other words, your program works. There is an exponential time increase when the grid gets bigger! My advice: don't worry, and just submit your solution. ;-) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Knight's Tour problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|