2

I was trying to make a knights tour problem solution and i have just made it. Now i want to improve it.It takes the starting value and then outputs the step by step instructions to move (in command line output).

Now the technique which i have used is that firstly i have divided the board into 4 blocks according to the solution given in a video (here www.youtube.com%2Fwatch%3Fv%3DdWM5pKYZCHw&b=28) and also divided the whole board into 4 systems of boxes.

In the solution i have to do do lots of backtracking to decide between two different possibilities which greatly reduces the speed.Is there any way to do less or no backtracking to decide between two possibilities. And any other suggestion to improve the technique. Here is a part of the code (a function which moves the knight across the board)

void move(int l[8][8][2],int g, int e)  // g and e are the required systems and blocks respectively
{
      backtracking(backtrackarray, l); // calling function to backtrack the array

      backtracking(secondbacktrackarray,l);    againcalling function to backtrack array in different array

      int system = currentsystem(l, currentposition[0], currentposition[1]); //storing the current system

      for (int i = 0; i < 3; i++)
      {
          nextmove(l, currentposition[0], currentposition[1]);  //moving knight

      }

      if (blockshiftpossible(l, system, currentposition[0], currentposition[1])!= 1) // checks if next block shift possible
      {
           backimage(l, backtrackarray); getting back the stored image 

           for (int i = 0; i < 3; i++)
           {
              reversenextmove(l, currentposition[0], currentposition[1]); // moving in the opposite way
           }

      }

      if ((systemshiftpossible(l, currentposition[0], currentposition[1])!= 1) && (g==4) && (e==4)) // checking if system shift is possible

      { 
            backimage(l,secondbacktrackarray); // getting again image from second backtrack array

            for (int i = 0; i < 3; i++)
            {

                 reversenextmove(l, currentposition[0], currentposition[1]);  // moving in opposite direction

            }

            if (systemshiftpossible(l, currentposition[0], currentposition[1])!= 1)
            {

                  for (int i = 0; i < 3; i++)
                  {
                     nextmove(l, currentposition[0], currentposition[1]);

                   }
             }
      }


      if ((blockshiftpossible(l, system, currentposition[0], currentposition[1])
        == 1) && (g!=4))

      {

            blockshift(l, currentposition[0], currentposition[1]);

      }

      else

      {

        cout << "logical error"<<endl;

      } 

}

To see the details of this technique check my previous question Solving knight tour with c++

Also how i can change it to get solutions for n*n puzzles if possible.

false
  • 10,264
  • 13
  • 101
  • 209
Ali Raza
  • 191
  • 2
  • 12
  • The magic word here is "recursion". Following that, look up "dynamic programming." – Charlie Martin Dec 13 '13 at 14:01
  • 1
    Yes i am thinking to convert it to dynamic programming. Actually i'm knew to c++ and i have just know learned dynamic programming so i first want to excel in dynamic allocation. – Ali Raza Dec 13 '13 at 14:04
  • How do i can get solution of n*n puzzles – Ali Raza Dec 13 '13 at 14:05
  • Ali, once you do a dynamic programming solution, n will just be a parameter. – Charlie Martin Dec 13 '13 at 16:31
  • http://vigneshmurugesan.com/2013/07/09/dynamic-programming-n-queen-problem/ will give you a crib if you need one. – Charlie Martin Dec 13 '13 at 16:33
  • No it will not give. Because the logic which is being used will not let that happen – Ali Raza Dec 16 '13 at 11:41
  • Ali, N is the parameter. – Charlie Martin Dec 16 '13 at 18:50
  • I understand what u are saying. N will be parameter and due to dynamic programming we can make matrix of our own choice but the point is that it wiil not give solutionn for puzzles greater than 8*8 or less than 8*8. Do you know about any solution which will give solution for other than 8*8. pLease let me know. – Ali Raza Dec 17 '13 at 06:42

0 Answers0