TOWER OF HANOI
/*
Tower of Hanoi Algorithm
TowerOfHanoi(noofdisk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
TowerOfHanoi (no-of-disk- 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
TowerOfHanoi (no-of-disk- 1, aux, dest, source) // Step 3
*/
#include<stdio.h>
void ToH(int,char,char,char);
void main()
{
int n;
printf("How many plates?");
scanf("%d",&n);
ToH(n,'S','D','A'); /*...Function call...*/
}
void ToH(int n,char x,char y,char z)
{
if(n > 0)
{
ToH(n-1, x, z, y);
/*..... n -1 plates moved from source to auxiliary using destination tower.....*/
printf("\n%c -> %c",x,y);
/*.....last plate moved foprm source to destination.... */
ToH(n-1, z, y, x);
/*.....n - 1 plates moved from auxiliary to destination using source tower......*/
}
}
/*
How many plates?3
S -> D
S -> A
D -> A
S -> D
A -> S
A -> D
S -> D
*/
Comments
Post a Comment