TOWER OF HANOIC PROBLEM> (RECURSION)
#include<stdio.h>
void tofh(int ndisk, char source, char temp, char dest);
main( )
{
char source = 'A', temp = 'B', dest = 'C';
int ndisk;
printf("Enter the number of disks : ");
scanf("%d", &ndisk );
printf("Sequence is :\n");
tofh(ndisk, source, temp, dest);
}/*End of main()*/
void tofh(int ndisk, char source, char temp, char dest)
{
if(ndisk==1)
{
printf("Move Disk %d from %c-->%c\n", ndisk, source, dest);
return;
}
tofh(ndisk-1, source, dest, temp);
printf("Move Disk %d from %c-->%c\n", ndisk, source, dest);
tofh(ndisk-1, temp, source, dest);
}
reference....
N = 0) has been met. To me the sheer simplicity of the solution is breathtaking. For N = 3 it translates into
N = 4 we get the following sequence
#include<stdio.h>
void tofh(int ndisk, char source, char temp, char dest);
main( )
{
char source = 'A', temp = 'B', dest = 'C';
int ndisk;
printf("Enter the number of disks : ");
scanf("%d", &ndisk );
printf("Sequence is :\n");
tofh(ndisk, source, temp, dest);
}/*End of main()*/
void tofh(int ndisk, char source, char temp, char dest)
{
if(ndisk==1)
{
printf("Move Disk %d from %c-->%c\n", ndisk, source, dest);
return;
}
tofh(ndisk-1, source, dest, temp);
printf("Move Disk %d from %c-->%c\n", ndisk, source, dest);
tofh(ndisk-1, temp, source, dest);
}
reference....
Recursive solution
Let call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). To better understand and appreciate the following solution you should try solving the puzzle for small number of disks, say, 2,3, and, perhaps, 4. However one solves the problem, sooner or later the bottom disk will have to be moved from Src to Dst. At this point in time all the remaining disks will have to be stacked in decreasing size order on Aux. After moving the bottom disk from Src to Dst these disks will have to be moved from Aux to Dst. Therefore, for a given number N of disks, the problem appears to be solved if we know how to accomplish the following tasks:- Move the top N - 1 disks from Src to Aux (using Dst as an intermediary peg)
- Move the bottom disk from Src to Dst
- Move N - 1 disks from Aux to Dst (using Src as an intermediary peg)
Solve(N, Src, Aux, Dst) if N is 0 exit else Solve(N - 1, Src, Dst, Aux) Move from Src to Dst Solve(N - 1, Aux, Src, Dst)This actually serves as the definition of the function Solve. The function is recursive in that it calls itself repeatedly with decreasing values of N until a terminating condition (in our case
- Move from Src to Dst.
- Move from Src to Aux.
- Move from Dst to Aux.
- Move from Src to Dst.
- Move from Aux to Src.
- Move from Aux to Dst.
- Move from Src to Dst.
- Move from Src to Aux.
- Move from Src to Dst.
- Move from Aux to Dst.
- Move from Src to Aux.
- Move from Dst to Src.
- Move from Dst to Aux.
- Move from Src to Aux.
- Move from Src to Dst.
- Move from Aux to Dst.
- Move from Aux to Src.
- Move from Dst to Src.
- Move from Aux to Dst.
- Move from Src to Aux.
- Move from Src to Dst.
- Move from Aux to Dst.
No comments:
Post a Comment