Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Every budding computer scientist must grapple with certain classic problems. The tower of Hanoi (see the Figure below) is one of the most famous of

Every budding computer scientist must grapple with certain classic problems. The tower of Hanoi (see the Figure below) is one of the most famous of these. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from on peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks. Supposedly, the world will end when the priests complete their tasks, so there is little incentive for us to facilitate their efforts. Figure: The Tower of Hanoi for the case with three disks. Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n-1 disks (hence, the recursion), as follows: (a) Move n-1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. (b) Move the last disk (the largest) from peg 1 to peg 3. 2 (c) Move the n-1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area. The process ends when the last task involves moving n=1 disk, i.e., the base case. This is accomplished by trivially moving the disk without the need for a temporary holding area. Write a program to solve the Towers of Hanoi problem and show your testing results for the case of moving a stack of five disks from peg 1 to peg 3. Use a recursive function (i.e., the function calls itself in the function body) with four parameters: (a) The number of disks to be moved. (b) The peg on which these disks are initially threaded. (c) The peg on which this stack of disks is to be moved. (d) The peg to be used as a temporary holding area. Your program should print the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should print the following series of moves; 1 --> 3 (This means move one disk from peg 1 to peg 3) 1 --> 2 3 --> 2 1 --> 3 2 --> 1 2 --> 3 1 --> 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

Do you currently have a team agreement?

Answered: 1 week ago

Question

c. How is trust demonstrated?

Answered: 1 week ago