void TowerOfHanoi (int disks, char* Left, char* Mid, char* Right ) {

    if (disks != 0 ) {
        //printf ("%d:: %s %s %s\n", i++, Left, Mid, Right);
        //call recursively..
        TowerOfHanoi(disks-1, Left, Right, Mid);
        printf ("\n%d: Move disk: %d from %s to %s\n", ++j, disks, Left, Right);
        TowerOfHanoi(disks-1, Mid, Left, Right);
    }
}
void TowerOfHanoi(int a, char from, char aux, char to) 
{
	if (a == 1)
	{
		printf("\nMove disc  %d  from  %c  to  %c \n", a, from, to);
		return;
	}
	else
	{
		TowerOfHanoi(a - 1, from, to, aux);
		printf("\nMove disc  %d  from  %c  to  %c \n", a, from, to);
		TowerOfHanoi(a - 1, aux, from, to);
	}
}
Ejemplo n.º 3
0
void TowerOfHanoi(int num, char src, char dest, char auxi)
{
    // Base case: for only 1 disk.
    if(num==0)
    {
     //   printf("Moving %d disk from %c  to %c  \n", num, src, dest);
        return;
    }
    //Recursive case: For more than 1 disks.
    TowerOfHanoi(num-1, src, auxi, dest);

    printf("Moving %d disk from %c to %c  \n", num, src, dest);

    //Moving n-1 disks from auxi to dest using src
    TowerOfHanoi(num-1, auxi, dest, src);
}
void main()
{
	int n;
	printf("\nTower of Hanoi\n");
	printf("\nEnter number of discs: ");
	scanf("%d", &n);
	TowerOfHanoi(n, 'S', 'A', 'D');
}
int main ()
{
    int disks;
    char* tower1 = "Left Tower ";
    char* tower2 = "Mid Tower  ";
    char* tower3 = "Right Tower";
    printf ("How many disks: ");
    scanf (" %d", &disks);
    printf ("Tower of Hanoi with disks: %d\n", disks);
    TowerOfHanoi(disks, tower1, tower2, tower3);
    return 0;
}
Ejemplo n.º 6
0
int main()
{
    int n=2;
    TowerOfHanoi(n, 'S', 'D', 'A');
    return 0;
}