Tower of Hanoi Problems

Source: Internet
Author: User

Tower of Hanoi. I think it may also be a problem that many people are confused about. Today, I don't know how to solve the problem that has plagued me for a long time. I hope it will be helpful.
As for the problem background, I would like to give a general introduction here,

To move a series of blocks from A to C, you can use B. Of course, the order of blocks cannot be changed, that is, small blocks must be placed on large blocks. What should I do now?

Recursion?That's right! It is recursion. How can we analyze it?

 

Now, you can think like this:

 

① If there is only one block, it can be directly moved from A to C, and the problem is solved.

 

② If there are n blocks, then I will move the n-1 blocks on A to B through C. Is it okay to move the largest block on a directly to C? ( The specific details of how to move n-1 blocks are not considered. Here is just an idea.)

 

③ Now, there are n-1 blocks on B, and A is empty. You need to move n-1-1 blocks to C through a. Let's take a look, is this step the same as ②, but the parameters have changed. Isn't this a typical application of recursion? In the end, when there is only one piece left, it is a recursive exit. Taking the three parts as an example, the following figure shows a mobile Graphic Process: The above is a graphic method, and it should not be too difficult to understand. But after all, the problem must be implemented by a program. Now we use algorithms to analyze the problem:
/** N move the number of blocks A, B, and C. Medium boards */Hanoi (n, a, B, c) 1 if n = 1 then2 move (a, c) 3 else4 Hanoi (n-1, A, C, B) 5 move (a, c) 6 Hanoi (n-1, B, A, C)

The specific analysis is as follows:

① The four parameters in the first line of the Code are represented as: Move n blocks on A to C through B. ② If n is 1, move them directly from A to C, this is a recursive exit ③ When n is not 1, first, we need to move the n-1 block on A to B through C ④ and then move one on A to C ⑤. Finally, we need to move the n-1 block on B to C OK through, now the problem is solved! Of course, when using algorithms to explain, it is just an idea that we cannot track every specific implementation. What should I do if I want to track the movements of each step? So I made a slight change to the Code so that the code is displayed every time I move it, as shown below:
# Include <stdio. h> void move (char X, char y) {printf ("% C --> % C \ n", x, y);} void Hanoi (int n, char, char B, char c) {If (n = 1) Move (a, c); else {Hanoi (n-1, A, C, B); move (, c); Hanoi (n-1, B, A, c) ;}} int main () {int N; printf ("Enter the number of blocks to move :"); scanf ("% d", & N); Hanoi (n, 'A', 'B', 'C'); Return 0 ;}

After such a small change, the program will be able to show how each step moves, and achieve the purpose of tracking and observation. Of course, if you are interested, you may wish to manually track the changes;

The running result is as follows: finally, after several tests, I found an interesting conclusion: When the tower is used to move blocks, the number of moves is 2 minus 1 (2 ^ n-1, where n For the number of blocks to be moved)If any, please advise. Note: The above code is compiled and run in the Code: blocks + GNU gcc Environment

 

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.