Part 1 Towers of Hanoi Every budding computer scientist must

Part 1:

(Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems, and the Towers of Hanoi is one of the most famous. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack has 64 disks stacked onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts.

Which by the way, will take 18,446,744,073,709,551,615 moves. If we assume one second per move, 24 hours a day, non-stop - it will take the monks 584,942,417,355 years to move all the disks from one peg to the other. In case you were wondering when the earth was ending.

Les us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers.

If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n – 1 disks (and hence the recursion) as follows:


The process ends when the last task involves moving n = 1 disk (i.e., the base case). This task is accomplished by simply moving the disk, without the need for a temporary holding area.

Write an program to solve the Towers of Hanoi problem. Allow the user to enter the number of disks. Use a recursive tower method with four parameters:

the number of disks to be moved,

the peg on which these disks are initially stacked,

the peg to which this stack of disks is to be moved (i.e., the final peg), and

the peg to be used as a temporary holding area.


Your program should output the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should print the follwing series of moves:

From peg to peg   
      1   --> 3
      1   --> 2
      3 --> 2
      1   --> 3
      2 --> 1
      2 --> 3
      1 --> 3

Solution

//tower of hanoi using recursion in C
#include <stdio.h>

void towerOfHanoi(int, char, char, char);

int main()
{
int num;

printf(\"Enter the number of disks : \");
scanf(\"%d\", &num);
printf(\"The sequence of moves in the Tower of Hanoi are :\ \");
//recursion call to the towerOfHanoi method with params
//the number of disks to be moved,
//the peg on which these disks are initially stacked,
//the peg to which this stack of disks is to be moved (i.e., the final peg), and
//the peg to be used as a temporary holding area.
towerOfHanoi(num, \'A\', \'C\', \'B\');
return 0;
}

//recursion method
void towerOfHanoi(int num, char fromRod, char toRod, char auxRod)
{
//base condiotion to return from the recursion call
if (num == 1)
{
printf(\"\ Move disk 1 from Rod %c to Rod %c\", fromRod, toRod);
return;
}
towerOfHanoi(num - 1, fromRod, auxRod, toRod);
printf(\"\ Move disk %d from Rod %c to Rod %c\", num, fromRod, toRod);
towerOfHanoi(num - 1, auxRod, toRod, fromRod);
}
------output--------
The sequence of moves in the Tower of Hanoi are :   
  
Move disk 1 from Rod A to Rod C
Move disk 2 from Rod A to Rod B
Move disk 1 from Rod C to Rod B
Move disk 3 from Rod A to Rod C
Move disk 1 from Rod B to Rod A
Move disk 2 from Rod B to Rod C
Move disk 1 from Rod A to Rod C
------output ends--------

Note: The output is more specific on how we want to move the disk i.e. it shows which disk to be moved from where to where.
feel free to ask question/doubts. God bless you!!

Part 1: (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems, and the Towers of Hanoi is one of the most famous. Legend
Part 1: (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems, and the Towers of Hanoi is one of the most famous. Legend

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site