Thursday, January 9, 2014

Knight's tour on N*N Square

Knight Tour Problem:- knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square exactly once. If the knight ends on a square that is one knight's move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open. The exact number of open tours on an 8x8 chessboard is still unknown.For more details...
                             
Great seems you understood the problem, again complex looking problem yes it is at first sight but not actually let's try to give a hit.

Naive Approach:-Let's start from any position (let's 0,0) on square..try to make any possible L(knight move), continue untill you filled all the places of square or either you don't have option to go ahead... Now this is the point to Backtrack from last positions of the knight hence doing back and forth you can achieve your destination...(yes it is time consuming).

Let's see the code here...
/*Developed By Atiqur Rahman for atiqwhiz.blogspot.in*/
#include <iostream>
#include <iomanip>
using namespace std;
#define R 8
#define C 8
class KnightTour
{
    int arr[R][C];
    int count;
    public:
     KnightTour():count(0)
     {  
         for(int n=0;n<R;n++)
          for(int m=0;m<C;m++)
          {
             arr[n][m]=0;
          }
       
     }
     bool Knight(int i,int j)
     {  
         count++;
        //cout<<"Count "<<count<<" i "<<i<<" j "<<j<<endl;
        if(count==R*C)
        {arr[i][j]=count;return true;}
       
        if(i+2<R&&j+1<C&&!arr[i+2][j+1])      //1
        {
            arr[i][j]=count;
            if(Knight(i+2,j+1))
             return true;
        }
       
        if(i+1<R&&j+2<C&&!arr[i+1][j+2])  //2
        {
            arr[i][j]=count;
           if(Knight(i+1,j+2))  
            return true;  
        }
       
        if(i-1>=0&&j+2<C&&!arr[i-1][j+2])  //3
        {
            arr[i][j]=count;
            if(Knight(i-1,j+2))
             return true;
        }
       
        if(i-2>=0&&j+1<C&&!arr[i-2][j+1])    //4
        {
            arr[i][j]=count;
            if(Knight(i-2,j+1))
             return true;
        }
         if(i-2>=0&&j-1>=0&&!arr[i-2][j-1])  //5
        {
            arr[i][j]=count;
            if(Knight(i-2,j-1))
             return true;
        }
   
         if(i-1>=0&&j-2>=0&&!arr[i-1][j-2])  //6
        {
            arr[i][j]=count;
            if(Knight(i-1,j-2))
             return true;
        }
       
         if(i+1<R&&j-2>=0&&!arr[i+1][j-2])   //7
        {
            arr[i][j]=count;
            if(Knight(i+1,j-2))
             return true;
        }
       
         if(i+2<R&&j-1>=0&&!arr[i+2][j-1])    //8
        {
            arr[i][j]=count;
            if(Knight(i+2,j-1))
             return true;
        }
         count--;
         arr[i][j]=0;
        return false;
     }
void put()
{
   
    cout<<"Resultant Matrix\n";
    for(int i=0;i<R;i++)
    {
        for(int j=0;j<C;j++)
        {
         cout<<setw(3)<<arr[i][j];  
        }
    cout<<endl;
    }

}

};

int main()
{
KnightTour k;
bool b=k.Knight(0,0);
k.put();  
if(b)
{
    cout<<"\nTour is Possible";
}
else
 cout<<"Tour is not possible";

}

//Happy coding....
//If you find any bug let us know.....Happy to help you......Must read this link for better understanding See this

No comments:

Post a Comment