2. Tower of Hanoi is a mathematical game or puzzle.
It consists of three rods, and a number of disks of different sizes which can
slide onto any rod.
The puzzle starts with the disks in a neat stack in ascending order of size on
one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying
the following rules:
1.Only one disk must be moved at a time.
2.Each move consists of taking the upper disk from one of the rods and
sliding it onto another rod, on top of the other disks that may already
be present on that rod.
3.No disk may be placed on top of a smaller disk.
3. The puzzle can be played with any number of disks.
The minimum number of moves required to solve a Tower of Hanoi puzzle is
2n - 1
where n is the number of disks.
Solution for Tower of Hanoi :For an even number of disks:
make the legal move between pegs A and B
make the legal move between pegs A and C
make the legal move between pegs B and C
repeat until complete
For an odd number of disks:
make the legal move between pegs A and C
make the legal move between pegs A and B
make the legal move between pegs C and B
repeat until complete.
In each case, a total of 2ⁿ-1 moves are made.
4. Algorithm
Input
:- Input of discs in Tower of Hanoi , specification of ORG as from the piller
and DES as to piller,INT as the intemediate piller.
Output :- Steps of moves of N discs from piller ORG to DCS piller.
Steps
1. if N>0 then
2.
move(N-1 ,ORG ,DES ,INT)
3.
ORG
DES (move from ORG to DES)
4.
move(N-1 ,INT ,ORG ,DES)
5. end if
6. stop
5. For N=3 , how will this recursion solve the problem as shown
Move(2,A,C,B)
A
B
Move(1,C,A,B)
Move(3,A,B,C)
A
C
Move(1,B,C,A)
Move(2,B,A,C)
B
C
MoveE(1,A,B,C)
C
( 1)
A
B
(2)
C
B
(3)
A
Move(1,A,B,C)
A
C
(4)
B
A
(5)
B
C
(6)
A
C
(7)