Solving the problem of Hanoi Tower

Source: Internet
Author: User
Tags define function

The former part of the article is reproduced, transferred from http://www.cnblogs.com/yanlingyin/

Of course, this is a classic recursive problem ~
Presumably to see this blog post of the classmate to Hanoi should not be unfamiliar with it,

Writing this blog still has the original intention:

Before learning the data structure of their own reading, also on the Internet to check a lot of information, the data are relatively scattered, and the description is not very clear, for the time just

Contact algorithm for me, to fully understand or have a certain degree of difficulty. Today just had time to tidy up the next idea, rewrite the analysis of the previous doubts about the place,

There is no clear place to be enlightened. So I can't wait to record my thoughts and share them with you.

If you are the same as I did before the Hanoi Tower can not fully digest, or just contact with Hanoi, it is hoped that my understanding of this way can give you some

Xu Help, if you feel that you have mastered the relatively solid, it can also be seen, there is a good idea can be shared, after all, Exchange discussion is also a good

Learning style.

Well, don't say much, cut into the chase.

About Hanoi origin Ah, legend Ah god horse is not wordy, we directly cut to the point:
Problem Description:

There is a Vatican tower, the tower has three seats a, B, c,a on the seat of Connaught Dry plate, plate size, large under, small on the ().

Move these plates from block a to block C, you can borrow B in the middle, but only one plate is allowed at a time, and in the course of the move, 3 seats

The sub always keeps the market in the lower, small plate on the top.

Description simplification: Move n plates on column A to column C, where B-pillars can be borrowed.

  

We directly assume that there are n plates:

Mark the plate from small to large 1, 2, 3......N

First, look at the status of the original question three pillars:
Status 0 A: N trays stacked in order. B: Empty. C: Empty.

The goal is to move the n plates on a to C. Because it has to be big on the next small, so the final result on the bottom of the C disk should be labeled N plate, Imagine:

To get the nth plate on a, take the n-1 plate off it. Where is it open? A total of three pillars: a clearly not, if placed on C

, then the biggest plate has no place to put, the problem is still not solved. So select column B. Of course, B is stacked according to the principle of small in the

(Remember: Don't worry about how to move, you can think of a function to complete the move, and now you don't have to consider how the function is implemented.) This is important).

It is obvious: the status of the three towers after the previous step:

State 1: A: Only the largest one plate. B: There are n-1 plates stacked by the rules. C is empty.

It's good to understand, well, actually it's half done here. (If you don't understand the front, please look again.) Point: Don't care how to move! )

We continue to:

At this time, you can directly move the largest disk on a to the C drive, the state after the move:

Intermediate state: A: Empty. B:N-1 a plate. C: There is a maximum plate (nth plate)

One thing to note is that the C-pillar at this time can actually be seen as empty. Since all the remaining plates are smaller than that, either of them can be placed on top of the C-pillar.

So now the state of three pillars:

Intermediate state: A: Empty. B:N-1 a plate. C: Empty

Think about it, now the problem and the original problem have some similarities? How is it more similar? Obviously, as long as the n-1 plate on the b moves to a, the problem to be solved is smaller than the original problem.

Now consider how to move the n-1 plate on the B to a, in fact, the moving method and the above to the n-1 of the disk from A to B is the same, but the name of the column is changed. (If you write a function, only the parameter call order changes).

Let's say you're done with the previous step (again, don't think about how to move, just think about using a function to do it), and look at the current state:

State 2: A: There are n-1 trays stacked in order. B: Empty. C: the nth plate stacked in order (can be seen as an empty column)

Just now, we have completed a perfect recursion. If you do not understand please look again, you can use the strokes out of three states, calm down the heart to slowly reasoning.

I have repeatedly stressed: when you want to move all the plates on the largest plate to another empty column, do not care about how to move, just think of it as a function can be done, do not care about the specific implementation of the function. If your ideas are tangled up here, it's hard to go deeper.

Here, in fact, the basic ideas have been cleared. State 2 and state 0, except for the smaller size, there is no difference in other aspects. And then just use the same way of thinking, you can go down deep ...

Well, let's see how to do it with algorithms:

The definition function Hanoi (a,b,c,n) means that the n plates on a are moved to C, where b can be used.

Define function Move (m,n) to move the plate on M to n

The problem we need to solve is Hanoi (a,b,c,n)//Status 0 above

1, move a n-1 on a to B:hanoi (a,c,b,n-1); Operation ended as State 1

2. Move the large plate on A to C move (A,C)

3. Move the n-1 on B to a Hanoi (b,c,a,n-1); Operation End Bit status 2 (only smaller than State 1)

If you do not understand it now, go back and look again, after all, if it is not easy for beginners to understand. You can write down a few key states with your pen, and see if you have a real commitment to seeing and thinking independently.

Give the algorithm C code:

Main () {intN; printf ("Please enter the number n to solve the N-order Hanoi tower problem: \ N"); scanf ("%d",&N); Hanoi (N,'A','B','C');}voidHanoiCharACharBCharCintN) {    if(n==1) {printf ("Move disk%d from%c to%c\n", N,a,c); }    Else{Hanoi (a,c,b, N-1); printf ("Move disk%d from%c to%c\n", N,a,c); Hanoi (B,a,c,n-1,); }}

Solving the problem of Hanoi Tower

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.