Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

Thank you for your helping.

Here is the code:

C
//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
Updated 12-Dec-10 9:08am
v4
Comments
Manfred Rudolf Bihy 12-Dec-10 15:08pm    
Edit:Added code tags, added category Algorithm, spelling and grammar
CPallini 12-Dec-10 16:25pm    
I probably didn't get you, but why don't you implement the Warnsdorff's algorithm?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900