One can image moving disks for problem size equal to 64 takes ages, because the total steps is one cannot finish moving considering each second moving 1 disk, during his/her entire life. (move directly from ‘A’ to ‘C’ for one disk). įrom the above algorithm, we have this recurrence formula:, with the terminal condition. ![]() So we may guess the total number of steps for h(n)is the total number of disks, is. We notice that the total steps for moving disks is 1, 3, 7, 15, 31 … for the problem size equal to 1, 2, 3, 4, 5 respectively. We can solve this by using Recursion: first move the top N-1 disks and then the N-th disk from A to C – finally move the N-1 disks to C. We can move the top n – 1 disks (treated as an individual) from rod ‘A’ to ‘B’, and move the disk n (the bottom, the largest disk) from rod ‘A’ to ‘C’, and finally move top n – 1 disks from ‘B’ to ‘C’, in this way, we move n disks from ‘A’ to ‘C’, using ‘B’ as the temporary rod. The problem looks complicated at first but it can be easily solved with recursion, decomposing the case n with a less-complicated situation of ( n – 1). And each time, it is only allowed to move one disk. In any time, a larger disk cannot be on top of a smaller one. The sizes of the disks are noted as 1 to n, 1 being the smallest and n being the largest. There are three rods, and all the disks are placed at the first one initially. ![]() Disks: 3 Moves: 0 Minimum Moves: 7 © 2021 v0. But you cannot place a larger disk onto a smaller disk. ![]() The problem can be described as moving a set of disks from one rod to another using a third rod as a temporary one. Tower of Hanoi Object of the game is to move all the disks over to Tower 3 (with your mouse). The problem of ‘Tower of Hanoi’ is a very classic problem/puzzle that is often used to teach recursion in Computer Science.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |