0% found this document useful (0 votes)
11 views

Tower of Hanoi

Uploaded by

Tanisha jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Tower of Hanoi

Uploaded by

Tanisha jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tower of Hanoi Problem:

History of Tower of Hanoi


• There is a story about an ancient temple in India (Some say it’s in Vietnam – hence
the name Hanoi) has a large room with three towers surrounded by 64 golden disks.

• These disks are continuously moved by priests in the temple. According to a prophecy,
when the last move of the puzzle is completed the world will end.

• These priests acting on the prophecy, follow the immutable rule by Lord Brahma of
moving these disk one at a time.

• Hence this puzzle is often called Tower of Brahma puzzle.


Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than
one discs is as depicted −

These discs are of different sizes and stacked upon in an ascending order, i.e. the smaller one
sits over the larger one. There are other variations of the puzzle where the number of discs
increase, but the tower count remains the same.

Rules
The mission is to move all the discs to some another tower without violating the sequence of
arrangement. A few rules to be followed for Tower of Hanoi are −

• Only one disc can be moved among the towers at any given time.
• Only the "top" disc can be removed.
• No large disc can sit over a small disc.

Tower of Hanoi puzzle with n discs can be solved in minimum 2n−1 steps it shows that a
puzzle with 3 discs has taken 23 - 1 = 7 steps.
Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem
with lesser number of discs, say → 1 or 2. We mark three towers with
name, source(A), destination(C) and aux(B) (only to help moving the discs). If we have only
one disc, then it can easily be moved from source to destination peg(or tower).
If we have 2 discs −

• First, we move the smaller (top) disc to aux peg.


• Then, we move the larger (bottom) disc to destination peg.
• And finally, we move the smaller disc from aux to destination peg.

So now, we are in a position to design an algorithm for Tower of Hanoi with more than two
disc. We divide the stack of discs in two parts. The largest disc (nth disk) is in one part and all
other (n-1) discs are in the second part.
Our ultimate aim is to move disc n from source to destination and then put all other (n1) discs
onto it. We can imagine to apply the same in a recursive way for all given set of discs.
The steps to follow are −
Step 1 − Move n-1 discs from A to B using C
Step 2 − Move nth disc from A to C
Step 3 − Move n-1 discs from B to C using A
A recursive algorithm for Tower of Hanoi can be driven as follows −

Void TOH (int n, int A, int B, int C)


{
If (n>0)
{
TOH (n-1, A,C,B)
Printf(“Move disc from %d to %d”,A,C)
TOH (n-1, B,A,C)
}
}
C program for tower of Hanoi Problem:
include<stdio.h>
//Function declaration
void TOH(int,int,int,int);
//Function definitions
void main()
{
int disks=3;
printf(“Follow these moves:\n”);
move(disks,1,2,3);
}
void move(int n,int A,int B,int C)
{
if(n>0)
{
move(n-1,A,C,B);
printf("Move disk from %d to %d\n",A,C);
move(n-1,B,A,C);
}
}

You might also like