Its easy only ....
tower of hanoi consist of three pegs peg A,peg B,peg C.
which is used as BEG,AUX,END
Let n be 5 for example...
wat u r going to do is
step 1 : move the top n-1 (4) disks from BEG to AUX...
in this case END will be used as auxiliary.
step 2: move the n th disk from BEG to END .
use AUX as auxiliary in this case
step 3: move the n-1(4) disks from AUX to END that is moved in step 1
use BEG as auxiliary in this step
Actual code is
void tower(int n,char beg,char aux,char end)
{
if(n>0)
{
tower(n-1,beg,end,aux); // move n-1 disks from beg to
aux
printf("%c->%c",beg,aux,end);//equivalent to
tower(1,beg,aux,end) move 1 disk from beg to end
tower(n-1,aux,beg,end) // move disks from aux to end
}
}
again that n-1 disks are moved to end which is called recursively.....
Still u have doubt???
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.