Tower of Hanoi
is a mathematical puzzle game which consists the number of rings in one plate
and there are two other plates’ temporary and destination. The player must place
the entire ring in destination from the source as in same order in source. Only one ring can be moved at a time. Temporary
plate can be used as a reference.
Algorithm
1. Start
2. Declare
and initialize necessary variables : disk size, source, destination , temporary
3. If
disk size = 0
·
Move disk ‘disk size’ from ‘source’ to ‘destination’
4. Else
move ‘disk size -1’ top disks from source to ‘temporary’ using destination
5. Move
remaining one disk from ‘source’ to ‘destination’
6. Move
(disk size – 1) top disks from ‘temporary’ to ‘destination’ using temporary
7. Repeat
until all disks finished
Program code for TOH in C
#include<stdio.h>
void TOH(int n,char s,char t,char
d)
{
if(n>0)
{
TOH(n-1,s,d,t);
printf("Move disk %d from %c to
%C\n",n,s,d);
TOH(n-1,t,s,d);
}
}
void main()
{
int n;
char s=('s');
char t=('t');
char d=('d');
printf("Enter number of disk");
scanf("%d",&n);
TOH(n,s,t,d);
}
No comments:
Post a Comment