![]() ![]() ![]() Line 15 calls the recursive solveTowers method which outputs the steps to the command prompt.įigure 15.16. In the main method (lines 616), line 12 creates a TowersOfHanoi object, passing as a parameter the total number of disks to be moved from one peg to another. Towers of Hanoi solution with a recursive method.Ģ // Program solves the towers of Hanoi problem, andħ int numDisks // number of disks to moveġ4 // recusively move disks through towersġ5 public void solveTowers( int disks, int sourcePeg, int destinationPeg,Ģ5 // recursion step - move disk to tempPeg, then to destinationPegĢ6 // move ( disks - 1 ) disks from sourcePeg to tempPeg recursivelyĢ7 solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg ) Ģ9 // move last disk from sourcePeg to destinationPegģ2 // move ( disks - 1 ) disks from tempPeg to destinationPegģ3 solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg ) įigure 15.16 tests our Towers of Hanoi solution. ![]() Line 33 finishes the rest of the moves by calling the method solveTowers to recursively move disks - 1 disks from the temporary peg ( tempPeg) back to the destination peg ( destinationPeg), this time using the first peg ( sourcePeg) as the temporary peg.įigure 15.15. When all but one of the disks have been moved to the temporary peg, line 30 records the step to move one disk, the largest one, from the start peg to the destination peg. In the recursion step (lines 2733), line 27 moves disks - 1 disks from the first peg ( sourcePeg) to the temporary holding peg ( tempPeg). The base case (lines 1923) occurs when only one disk needs to be moved from the starting peg to the ending peg. Method solveTowers (lines 1534) solves the Towers of Hanoi puzzle given the total number of disks (in this case 3 ), the starting peg, the ending peg, and the temporary holding peg as parameters. In the constructor (lines 912), the number of disks to be moved ( numDisks) is initialized. This task is accomplished by simply moving the disk, without the need for a temporary holding area.įigure 15.15 displays the precise instructions it will take to move the disks from the starting peg to the destination peg. The process ends when the last task involves moving n = 1 disk (i.e., the base case). Move the n - 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.Move the last disk (the largest) from peg 1 to peg 3.Move n - 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.Moving n disks can be viewed in terms of moving only n - 1 disks (hence the recursion) as follows: Instead, attacking this problem with recursion in mind allows the steps to be simple. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. We wish to develop an algorithm that prints the precise sequence of peg-to-peg disk transfers. Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. Towers of Hanoi for the case with four disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts.įigure 15.14. Three pegs are provided, one being used for temporarily holding disks. The priests are attempting to move the stack from one peg to another 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. The initial stack has 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. Legend has it that in a temple in the Far East, priests are attempting to move a stack of golden disks from one diamond peg to another (Fig. The Towers of Hanoi is one of the most famous classic problems every budding computer scientist must grapple with. ![]() In this section, we present a problem whose recursive solution demonstrates the elegance of recursion, and whose iterative solution may not be as apparent. In the preceding sections of this chapter, we studied methods that can be easily implemented both recursively and iteratively. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |