GE8151 U1 15 Towers of Hanoi

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Problem Solving and Python Programming GE8151

Towers of Hanoi

The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower[1] and sometimes
pluralized) 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
simple rules:

Rule 1: Only one disk can be moved at a time.

Rule 2: Each move consists of taking the upper disk from one of the stacks and placing it on top
of another stack or on an empty rod.

Rule 3: No disk may be placed on top of a smaller disk.

Disk 1

Disk 2

Disk 3
Source Destination Aux

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Towers of
Hanoi Playing
with 1 Disks
Disk 1

Source Destination Aux


Move 1
Disk 1 Move S to D

Total number of
moves is 1

Source Destination Aux

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Towers of
Hanoi Playing
with 2 Disks
Disk 1

Disk 2

Source Destination Aux

Move 1
Disk 1 Move S to
A

Source Destination Aux

Move 2
Disk 2 Move S to
D

Source Destination Aux


Move 3
Disk 1 Move A to
D
Total number of
moves is 3

Source Destination Aux

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Towers of
Hanoi
Playing with
3 Disks
Disk 1

Disk 2

Disk 3
Source Destination Aux

Move 1
Disk 1 Move S
to D

Source Destination Aux

Move 2
Disk 2 Move S
to A

Source Destination Aux

Move 3
Disk 1 Move D
to A

Source Destination Aux

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Towers of
Hanoi Playing
Move 4 with 3 Disks
Disk 3 Move S to
D

Source Destination Aux

Move 5
Disk 1 Move A to
S

Source Destination Aux


Move 6
Disk 2 Move A to
D

Source Destination Aux

Move 7
Disk 1 Move S to
D
Total number of
moves is 7

Source Destination Aux

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Algorithm

Move Disks

Step 1 Start Process


Step 2 Read number of disks
Step 3 Call Move Disks with number of disks, source, destination, aux
Step 4 Stop Process

Move Disks
Step 1 Start Process
Step 2 Receive number of disks and store in n
Step 3 If n is equal to 1 then Move disk from source to destination and goto Step 16
Step 4 Else goto Step 5
Step 5 Assign disks as n-1
Step 6 Assign source as source
Step 7 Assign destination as aux
Step 8 Assign aux as destination
Step 9 Call Move Disks with disks, source, destination, aux
Step 10 Move disk n from source to destination
Step 11 Assign disks as n – 1
Step 12 Assign source as aux
Step 13 Assign destination as destination
Step 14 Assign aux as source
Step 15 Call Move Disks with disks, source, destination, aux
Step 16 Stop Process

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Flow Chart

Main Flow Chart

start

read no of disks

no of disks, source, destination, aux

Move Disks

stop

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Move Disks Flow Chart

start

recive disks

disks, source, destination, aux

disks = 1 False
?
True
move disks from source to destination

disks - 1, source, aux, destination

Move Disks

move disks from source to destination

disks - 1, aux, destination, source

Move Disks

stop

Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151

Pseudo Code

Main Pseudo Code

START
READ number of disks
CALL move_disks(no of disks, source, destination, aux)
STOP

Move Disks Sub Routine PseudoCode

START
PROCEDURE: move_disks(disks, source, destination, aux)
IF disks equal to 1
Move Disks from source to destination
ELSE
CALL move_disks(disks - 1, source, aux, destination)
Move Disks from source to destination
CALL move_disks(disks - 1, aux, destination, source)
END IF
END PROCEDURE
STOP

Rajasekaran S AP/IT

You might also like