13,141,286 members (77,937 online)
Rate this:
See more:
I've got to code the knight tour problem in which a knight visits every square on a chessboard while obeying the rules in which a knight is allowed to move in a chess game. When this code runs, it is random variable(so run with definite path). I would like to specify the knight's coordinate and the program shall calculate it's movement. How can I do that?

Here is the code:

```//knight movement problem in chessboard
#include<iostream>
using namespace std;

int p[8][8],atama=1;
int seviye=8;
int x,y;
int recursive(int f,int g)
{
int a=f,b=g;
int count=0;

if((a>=0&&b>=0)&&(a<8&&b<8))
{
if((a-2>=0&&b+1<=7)&&(p[a-2][b+1]==0))
count++;
if((a-2>=0&&b-1<=7)&&(p[a-2][b-1]==0))
count++;
if((a-1>=0&&b+2<=7)&&(p[a-1][b+2]==0))
count++;
if((a-1>=0&&b-2<=7)&&(p[a-1][b-2]==0))
count++;
if((a+2>=0&&b+1<=7&&a+2<=7)&&(p[a+2][b+1]==0))
count++;
if((a+1>=0&&b+2<=7&&a+1<=7)&&(p[a+1][b+2]==0))
count++;
if((a+1>=0&&b-2<=7&&a+1<=7)&&(p[a+1][b-2]==0))
count++;
if((a+2>=0&&b-1<=7&&a+2<=7)&&(p[a+2][b-1]==0))
count++;
}
// a=f,b=g;
if(count<seviye)>
{
seviye=count;
x=f;
y=g;
}
}

void appointment(int i,int j)
{
int a=i,b=j;
if(atama<=64)
{
if(p[a+2][b-1]==0)
if(!(a+2<0||a+2>=8))
if(!(b-1<0||b-1>=8))
recursive(a+2,b-1);
if(p[a+2][b+1]==0)
if(!(a+2<0||a+2>=8))
if(!(b+1<0||b+1>=8))
recursive(a+2,b+1);
if(p[a-1][b+2]==0)
if(!(a-1<0||a-1>=8))
if(!(b+2<0||b+2>=8))
recursive(a-1,b+2);
if(p[a+1][b+2]==0)
if(!(a+1<0||a+1>=8))
if(!(b+2<0||b+2>=8))
recursive(a+1,b+2);
if(p[a-2][b+1]==0)
if(!(a-2<0||a-2>=8))
if(!(b+1<0||b+1>=8))
recursive(a-2,b+1);
if(p[a-2][b-1]==0)
if(!(a-2<0||a-2>=8))
if(!(b-1<0||b-1>=8))
recursive(a-2,b-1);
if(p[a+1][b-2]==0)
if(!(a+1<0||a+1>=8))
if(!(b-2<0||b-2>=8))
recursive(a+1,b-2);
if(p[a-1][b-2]==0)
if(!(a-1<0||a-1>=8))
if(!(b-2<0||b-2>=8))
recursive(a-1,b-2);

p[i][j]=atama++;
seviye=8;
appointment(x,y);
}

}

void print()
{
for(int i=0;i<8;i++)
{
cout<<endl;
cout<<"      ";
for(int j=0;j<8;j++)
cout<<p[i][j]<<"  ";
cout<<endl;
}
cout<<endl<<endl;
}

main()
{
appointment(7,7);
print();
system ("pause");
}```
Posted 12-Dec-10 8:48am
zoint384
Updated 12-Dec-10 9:08am
v4
ManfredRBihy 12-Dec-10 15:08pm

CPallini 12-Dec-10 16:25pm

I probably didn't get you, but why don't you implement the Warnsdorff's algorithm?

Top Experts
Last 24hrsThis month
 Graeme_Grant 187 Jochen Arndt 185 OriginalGriff 185 Karthik Bangalore 110 CPallini 100
 OriginalGriff 5,032 Graeme_Grant 4,970 ppolymorphe 1,744 Jochen Arndt 1,707 CPallini 1,490