5/5 – (8 votes)
1. The Tower of Hanoi(a) Compile and run the JSS implementation (on page 598 600) of the Tower of Hanoi.You should have three pegs (from left to right peg0, peg1, and peg2). Disks however are (from the smallest to the largest) disk1, disk2, , diskn. All the desks are stacked on peg0 initially. It is asked to move all disks from peg0 to peg2. The output for the program with 3 disks is:
Move one disk from 0 to 2Move one disk from 0 to 1Move one disk from 2 to 1Move one disk from 0 to 2Move one disk from 1 to 0Move one disk from 1 to 2Move one disk from 0 to 2
(b) Revise your implementation to include stepwise states of the tower, like the following. Initially:
peg0: 3 2 1peg1: 0 0 0peg2: 0 0 0Step 1: Move disk1 from peg0 to peg2 resultingpeg0: 3 2 0peg1: 0 0 0peg2: 1 0 0Step 2: Move disk2 from peg0 to peg1 resultingpeg0: 3 0 0peg1: 2 0 0peg2: 1 0 0Step 3: Move disk1 from peg2 to peg1 resultingpeg0: 3 0 0peg1: 2 1 0peg2: 0 0 0Step 4: Move disk3 from peg0 to peg2 resultingpeg0: 0 0 0peg1: 2 1 0peg2: 3 0 0Step 5: Move disk1 from peg1 to peg0 resultingpeg0: 1 0 0peg1: 2 0 0peg2: 3 0 0Step 6: Move disk2 from peg1 to peg2 resultingpeg0: 1 0 0peg1: 0 0 0peg2: 3 2 0Step 7: Move disk1 from peg0 to peg2 resultingpeg0: 0 0 0peg1: 0 0 0peg2: 3 2 1
(c) We have 10 disks, i.e., Initially:
Reviews
There are no reviews yet.