Ok this is the classic Towers of Hanoi question. Legend has it that in a temple in the Far East, priest are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks.
Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that prints the precise sequence of peg-to-peg disk transfers.
I have the program and it runs perfect. I couldn't figure it out but I found it online ( I know some disagree with this but I tried for a while to figure this out and I couldn't. I'm trying to understand the logic behind it, not just copying the code ). My only problem is how the recursive function executes. I don't understand it's path for the variable n. I understand recursion breaks down a problem to a base. This one just seems weird. I even watched the variable n in the watch window. ( I use visual studio 2008 ). I also searched the forums for this problem and I saw a good amount of threads but none helped with my confusion.
When it first enters the function ( assume I choose 3 for number of disks ). After step 3 it exits the function but then something weird happens. It re-enters the function with n value of 1 and then it continues to the cout statement and outputs #1 disk.
if( n > 0 ) check for if step1, check if step2, check if step3, check if step8
{
move( n-1,s,d,i ); step1, n-1 n = 2, step2 n-1 n=1, step3 n-1 n=0, step5 reenter with n=1, step10 n=2 at this line
// move n-1 disks from source to intermediate tower
cout << "disk " << n << " is moved from " << s << " to " << d << endl; step6 output n=1 value, step11 output n=2
// move the disk from to source to destination
move( n-1,i,s,d ); step7 n-1 n= 0, step10 re-enter with n=1
// move n-1 disks from intermediate to destination
}
} step4 exit function, step9 exit function,
And the function continues along it's path until it solves the Towers of Hanoi problem. I can't follow this recursive function and I really want to understand the logic behind it. Also the way the variables s d i are swapped lost me too as far as printing the correct tower to move the disks to. So if anyone could help explain it would be greatly appreciated. I want to understand this program fully before I move on. Here is the full program:
#include<iostream>
using namespace std;
void move( int n, char*s, char*i, char*d )
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if( n > 0 )
{
move( n-1,s,d,i );
// move n-1 disks from source to intermediate tower
cout << "disk " << n << " is moved from " << s << " to " << d << endl;
// move the disk from to source to destination
move( n-1,i,s,d );
// move n-1 disks from intermediate to destination
}
}
void main()
{
cout << "\n************************************************ **********\n";
cout << "This C++ program is to solve the towers of hanoi problem";
cout << "\n************************************************ **********\n";
cout << "Enter the no. of disks ";
int n;
cin >> n;
move( n, "source tower", "intermediate tower", "destination tower" );
}