0% found this document useful (0 votes)
34 views3 pages

Handouts LectureSlides Lecture5 6

The document describes the Towers of Hanoi puzzle. It involves moving a stack of 64 discs of different sizes between three spikes, where discs must be moved one at a time and larger discs cannot rest on top of smaller discs. It suggests solving the puzzle recursively by breaking it down into smaller subproblems of moving fewer discs and building up a solution. An example Python program is provided that uses recursion to print out the necessary moves.

Uploaded by

priyanka
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)
34 views3 pages

Handouts LectureSlides Lecture5 6

The document describes the Towers of Hanoi puzzle. It involves moving a stack of 64 discs of different sizes between three spikes, where discs must be moved one at a time and larger discs cannot rest on top of smaller discs. It suggests solving the puzzle recursively by breaking it down into smaller subproblems of moving fewer discs and building up a solution. An example Python program is provided that uses recursion to print out the necessary moves.

Uploaded by

priyanka
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

Towers

of Hanoi
The story:
3 tall spikes
Stack of 64 dierent sized discs start on one
spike
Need to move stack to second spike (at which
point universe ends)
Can only move one disc at a Dme, and a larger disc
can never cover up a small disc

Towers of Hanoi
Having seen a set of examples of dierent
sized stacks, how would you write a program
to print out the right set of moves?
Think recursively!
Solve a smaller problem
Solve a basic problem
Solve a smaller problem

def printMove(fr, to):!


print('move from ' + str(fr) + ' to ' + str(to))!
!
def Towers(n, fr, to, spare):!
if n == 1:!
printMove(fr, to)!
else:!
Towers(n-1, fr, spare, to)!
Towers(1, fr, to, spare)!
Towers(n-1, spare, to, fr)!

You might also like