We can write the solution for this puzzle in both iterative and recursive approaches. toh(n-1, source, aux, dest)Ģ: Move the nth disk from source to destinationģ: Move n-1 disks from aux to dest i.e. Thus, in general, for n disks, the steps are:ġ: Move n-1 disks from source to auxiliary i.e. Step 2: Move the disk from source to destination Consider a puzzle with 3 pillars and 3 disks as shown: Tower of Hanoi is a recursion based puzzle and thus, we will follow a recursive approach to solve it. We can only place a disk above a disk of a larger size. Our objective in this puzzle is to move all these disks to another pillar without changing the order in which the disks are placed in the initial state. Then it can move disks from any of the three towers to any of the others.Keeping you updated with latest technology trends, Join TechVidvan on Telegram Rules to be followed: I can have my method take in values just as the moveSingleDisc() method did. Note that I'm not confined to always moving discs from tower 0 to tower 1. You can try following these steps yourself using the applet above, or the alternate Towers of Hanoi applet. You are given a set of three pegs and disks, with each disk a different size. Then, in the third call all I had left to do was move the smaller disk back from tower 2 onto tower 1. #The hanoi towers freeThen in the second call to "moveSingleDisc" I was free to move the remaining disk on tower 0 (the larger disk) from tower 0 to tower 1. Step 3: toh (2, aux, dest, source) Thus, in general, for n disks, the steps are: 1: Move n-1 disks from. Consider a puzzle with 3 pillars and 3 disks as shown: Step 1: toh (2, source, aux, dest) Step 2: Move the disk from source to destination. What did this do? I moved the smallest disk from tower 0 to tower 2, my spare tower, to get it out of the way. Tower of Hanoi is a recursion based puzzle and thus, we will follow a recursive approach to solve it. Of course, the larger disk can never be placed on top of the smaller disk. Consider a two-disk game were I want to move 2 disks from tower 0 onto tower 1. I can now use this method to write an algorithm for solving the Towers of Hanoi game. So, if I were to call moveSingleDisc(0, 1, 2) it would move the top disk from tower 0 onto the top of tower 1. This will take the top disk from the from tower, move it to the to tower, and leave the third tower spare unaffected. MoveSingleDisc(int from, int to, int spare) Also, suppose we have the following method: For this lab, assume that we're playing the Towers of Hanoi game with 3 towers labeled 0, 1, and 2. #The hanoi towers codeWe'll do something similar here, we just won't type in any code to Eclipse. Towers of Hanoi Also called the Towers of Brahma or Towers of Lucas is a mathematical game, puzzle or toy that is made of three pegs (poles) and a number of disks of different sizes (in its basic form consisting of three disks), which can be moved into any peg (Hofstadter, 1996). In previous labs we've been trying to get used to the idea of calling methods to get work done, often with those methods having been written by someone else. According to the legend, before the monks could make the final move to complete the new pile in the new location, the temple would turn to dust and the world would end.īelow is an applet program representing the task of moving a pile of disks from one tower to another. Using the intermediate location, the monks began to move disks back and forth from the original pile to the pile at the new location, always keeping the piles in order (largest on the bottom, smallest on the top). In addition, there was only one other location in the temple (besides the original and destination locations) sacred enough for a pile of disks to be placed there. A disk could not be placed on top of a smaller, less valuable disk. The disks were fragile only one could be carried at a time. In an ancient city, so the legend goes, monks in a temple had to move a pile of 64 sacred disks from one location to another. Using recursion to solve the Towers of Hanoi problem.<< Return to Homework Page Lab 9 : Recursion Lab Overview
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |