ballssraka.blogg.se

Recursive hanoi towers
Recursive hanoi towers




recursive hanoi towers

base case: n is 0, so the tower is solved just return

recursive hanoi towers

So, a skeleton function will look like: hanoi_solve(int num_disks, int destination, int source, int intermediate) Of course, in between them, you're going to have to move the big disk (like the 'hint' says), and that's either going to involve printing to the screen or updating your data structures. Your recursive solution is going to involve two calls to your Hanoi function: one of them moving all of the disks to the central tower, and one of them moving all of the disks from the central tower to the end tower. The recursive nature of the Hanoi towers is such that it breaks down to the 'hint' that you get there. this 'hint' doesn't do a good job clarifying anything!īasically, imagine that you have numerical labels for each of the three tables. Just a shot in the dark, is this problem from a Deitel & Deitel book (with all the anthropomorphized ants)? I remember coming across this (almost exact same wording) problem fairly early on in my programming career, and. How does the method handle which stack the disks need to go on for sizes smaller than n? How does it complete the stack on the destination tower after moving the base disk to the destination tower?Ĭould anyone please explain this? Recursive thinking is definitely a weak spot for me. I still couldn't make this method on my own with this explanation. get the 2 disks >onto the final tower using the start tower as the intermediate tower).īut it hasn't helped. Then again solve the problem for n - 1 disks but use >the intermediate tower instead of the start tower (i.e. Then, move the biggest disk from start tower >to final tower. get 2 disks onto the >intermediate tower). 2 disks) using the intermediate >tower instead of the final tower (i.e. To move n disks (or think of, say, 3 disks): Solve the problem for n - 1 disks (i.e. Move smaller disk from start tower to intermediate tower, move larger >disk from start tower to final tower, move smaller disk from >intermediate tower to final tower and we are done. I just read this: Lets start with 1 disk (our base case): Move 1 disk from start tower to destination tower and we are done.

#RECURSIVE HANOI TOWERS HOW TO#

I just don't see it and I know how to solve Towers of Hanoi problems. Can someone explain this to me like I'm an idiot? This is not for an assignment or any sort of homework, but in my Data Structures course they continually talk about how the towers of hanoi problem is solved most easily by recursion.






Recursive hanoi towers