Hanoi algoritm first recursion step?

给你一囗甜甜゛ 提交于 2021-01-29 20:13:59

问题


I know this algoritm in theory.

Lets assume we have three disks and we want to move disks from A to C rods.

First move n-1 disks from A to B using C Then move last big disk to C After all move all disks from B to C using A

I dont get why using fist recursion honoi(n, A, C, B) we move all disks from A to C.

Lets make iterations:

First step:

n - 1 -> 2, it means we get a second disk from stack of disks:

Disk 1
Disk 2 -> get this
-------
Disk 3

And move it to the empty rod:

Disk 1       Destination rod    
------ 
Disk 3                        Disk 2

Second step:

We get next dist Disk 1

------       Destination rod    Disk 1
Disk 3                          Disk 2

Third step: Then move Disk 3 to Destination rod"

-----  Disk 3   Disk 1
                Disk 2

What do we next?

We continue the same operation for Disk 1, Disk 2 using empty rod, excluding Disk 2 because n - 1 right?


Start again from n - 1 for third rod, after movement it comes to:

Disk 1 Disk 3 Disk 2

We need move all to the second rod, right?

Step 1:

Disk 1 Disk 2
       Disk 3

Step 2 (finish):

   Disk 1
   Disk 2
   Disk 3

So, I can not get how we know that we must move from the third rod and second step from the first rod to complete algoritm?

Do you know how to visualizate this algoritm by steps? I dont get how we choose empty rod...


回答1:


@MattTimmermans is, of course, correct in stating that recursion is not necessary to solve the Towers of Hanoi. But I think that the standard recursive solution is elegant and reveals the structure of the solution. So it's worth spending a few minutes coming to grips with it.

First of all, the following statement from your question is not a correct interpretation:

First step: n - 1 -> 2, it means we get a second disk from stack of disks:

That's not what it means. hanoi(n, A, B, C) means "move all the disks from A to C using B as a temporary holding space. It does not mean "move disk N alone", which you cannot do because the rule of the game is that you can only move a disc which has nothing on top of it.

So what the recursive solution says is:

To move n discs from the source peg to the destination peg (using the other peg as a temporary):

  1. Move the top n−1 discs to the temporary peg.

  2. Move the disc n (which now has nothing on top of it) to the destination peg.

  3. Move the n−1 discs from the temporary peg (where we put them at step 1) to the destination peg using the original peg as a temporary.

That's it.

What makes that solution recursive is that it doesn't spell out how to actually implement steps 1 and 3. (Step 2 is just a simple move.) We do steps 1 and 3 by using the same algorithm, except with one fewer disc. In effect, while we are performing steps 1 and 3, we are ignoring disc n (and bigger discs, if any). (Or, if you prefer, we are pretending that they are the floor.) That works out fine because they are all bigger than any of the discs we are moving, so they don't prevent us from putting one of the discs we are working on on top of them.

So, to move three discs:

Start: Goal is to move 3 discs from A to C
    ┌─┐ 
    └─┘ 
   ┌───┐ 
   └───┘ 
  ┌─────┐ 
  └─────┘    
 ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀

1. Move 2 discs from A to B (using C)


                 ┌─┐ 
                 └─┘ 
  ┌─────┐       ┌───┐ 
  └─────┘       └───┘     
 ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀

2. Move disc 3 from A to C


                 ┌─┐ 
                 └─┘ 
                ┌───┐       ┌─────┐
                └───┘       └─────┘
 ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀

3. Move 2 discs from B to C (using A)
                              ┌─┐ 
                              └─┘ 
                             ┌───┐ 
                             └───┘ 
                            ┌─────┐ 
                            └─────┘    
 ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀

If for some reason the recursion seems confusing to you, pretend that you are doing the Towers of Hanoi and you have a friend who will help you. So you start by reading the instructions for moving three disks from peg A to peg C, and they say that the first thing you need to do is to move two disks from peg A to peg B. But you don't know how to do that, so you call your friend and they move the top two disks from peg A to peg B, so that you're in the second diagram above. Now the instructions say to move disk 3 from peg A to peg C, which you can do because disk 3 is now uncovered. So now you're at the third diagram, and again you have to move two disks. So you call your friend over again, and they move two disks from peg A to peg C for you, and the result is the bottom diagram, which means the problem has been solved.

So how did your friend manage to do that? Well, they are reading the same instructions as you are, so when you ask them to move two disks from peg A to peg B, they need to first move one disk (two minus one, right?) from peg A to their temporary peg, which is peg C. But that's easy, so they move the one peg. Then the instructions say to move the disk from peg A to peg B; again, no problem. And then they have to move the one disk from peg C to peg B. And the result is that they have moved two disks from A to B. Enhorabuena.

Now, let's step back a bit. Suppose you're the friend of someone who has been asked to solve the Tower of Hanoi with four disks, as with this very pretty animation on the Wikipedia page. Of course, they have the same instructions, so they had to start by moving three disks from peg A to peg B, and of course they asked you to do that, because you already have practice at moving three disks. So you do that for them, and then they lift disk 4 off of peg A and place it onto peg C. And then they call you back to move three disks from peg B to peg C.

Now, here's the cool part: You don't really need three people to do this. All you need is a bunch of post-it notes. Each time you have a subgoal, instead of finding a friend, you just write down what your current goal is and what step you are at in the instructions. Then you put the post-it note on top of the pile of post-its, and pretend you're the friend who has to do the move with one fewer disk. When you finish that task, you take the post-it off the top of the pile, and resume where you left off, tossing the post-it into the garbage.

That's recursion. It's simply a way of saying that when you solve a problem, you often have to do it pieces. And sometimes the solution to the pieces uses essentially the same method, but with smaller inputs.

Why don't you try it yourself? All you need is a few pieces of paper to represent the disks, and some post-it notes (or more bits of paper) to help your memory of where you are. Doing it by hand won't take you too long, and it's a lot easier to understand than reading words written in a foreign language.




回答2:


I'm not sure what your question is, but here's my best way to visualize the moves in the towers of hanoi:

Arrange the pegs in a triangle like this:

     A     C

        B

Now, each time you move a disk, it's going to move either clockwise or counter-clockwise.

RULE 1: A single disk will move in only one direction -- clockwise or anti-clockwise.

RULE 2: Alternate disks move in opposite directions.

That means that if disk 1 (the smallest) moves clockwise, then 1, 3, 5, 7... all move clockwise, and 2, 4, 6, 8, ... all move anti-clockwise.

It also means that if the you have an even number of disks, then the largest and smallest move in opposite directions. Otherwise they move in the same direction. This makes it easy to figure out which way the first move goes.

RULE 3: Always move the biggest disk you can, in its proper direction.

Rule 3 determines the pattern of which disk moves. It's 1, 2, 1, 3, 1, 2, 1, ... You start with all 1s, and replace every 2nd 1 with a 2. Then replace every 2nd 2 with a 3, etc. If you count in binary with a bit for each disk: 000 001 010 011 100... then at each step, the highest bit that changes corresponds to the largest disk that moves. If you start at all 0s, then you're done when you get to all 1s.

These rules actually make it very easy to solve the problem without any recursion at all. They also teach something about recursive vs. iterative solutions: The recursive solution is usually easier to formulate and understand. The iterative solution is often more efficient, but requires a deeper insight.



来源:https://stackoverflow.com/questions/62032087/hanoi-algoritm-first-recursion-step

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!