Click here to Skip to main content
13,141,286 members (77,937 online)
Rate this:
 
Please Sign up or sign in to vote.
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:

//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
Comments
ManfredRBihy 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 12 Dec 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100