Towers of Hanoi problem: There is a board with three pegs and three disks of dif
ID: 3809515 • Letter: T
Question
Towers of Hanoi problem: There is a board with three pegs and three disks of different sizes (see Figure 1). The goal of the game is to move the three disks from the peg where they have been placed (largest disk on the bottom, smallest disk on the top) to one of the empty pegs, subject to the following constraints:
a. Only the top disk on a peg can be moved to another peg.
b. A larger disk cannot be placed on top of a smaller disk.
Design a PSEUDOCODE algorithm to handle the Towers of Hanoi problem.
Figure 1: Towers of HanoiExplanation / Answer
FUNCTION HanoiTower(disk, source, dest, spare): IF disk == 0, THEN: move disk from source to dest ELSE: HanoiTower (disk - 1, source, spare, dest) // Step 1 above move disk from source to dest // Step 2 above HanoiTower (disk - 1, spare, dest, source) // Step 3 above END IF
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.