title: Tower of Hanoi [Edit] - k peg solution
body: <p>I was able to come up with an algorithm (logical) to solve a k-peg solution for the Tower of Hanoi problem, but when I implement my code, I was getting segmentation fault.</p>
  
  <pre><code>void move(int number_of_disks, int source, int dest, vector &lt;int&gt; free_peg, int pointer)
      {
          int p;
  
          if (1 == number_of_disks)
          {
              moves++;
              move_top_disk (source, dest);
          }
  
          if(free_peg.size() &gt; 2)
              p = number_of_disks/2;
          else 
              p = number_of_disks - 1;
  
          moves++;
          //Move top "p" disks from peg 1 to peg i
          move(p, source, free_peg.back(),free_peg, pointer); 
          //Move "n - p - 1" disks from peg 1 to another peg 
          move(number_of_disks - p - 1, source, free_peg[pointer--], free_peg, pointer++);
          //Move the "last disk" from the source peg to the destination
          move_top_disk(source, dest);
          //Move "n - p - 1" disks from peg (i - 1) to the final peg
          move(number_of_disks - p - 1, free_peg[pointer--], dest, free_peg, pointer++);
          //Move "p" disks from peg i to the destination
          move(p, free_peg.back(), dest, free_peg, pointer);
      }
  </code></pre>
  
  <p>The idea is pretty simple, I keep a vector of free pegs (or towers) and I update that when I move my disks throughout. So for the case of 6 pegs and n disks, I have one source, one destination and 4 free pegs. The idea is to move (n - p) where p ~ n/2 from the source to free_peg[3] (the fourth free peg). Now I only have 3 free pegs in my vector, I use those 3 free pegs to move (n - p - 1) disks to free_peg[2] and then I move the last disk from the source to the destination. So now I have 2 free pegs and 1 source = 3 free pegs. Next, I need to move (n - p - 1) disks from peg[2] to the destination using 3 free pegs (including the source which is now free). Finally, move p disks from free_peg[3] to the destination using 4 free pegs. However, when I implement this in my code, I am getting a segmentation fault, can someone please help me with this?</p>