The final solution to the problem of Hanoi Tower

Source: Internet
Author: User
Tags final

Question: At the end of the 19 century, an intellectual toy was sold in stores in Europe, with three poles on a single copper plate, and a column on the left pole, from top to bottom, with a 64-disc tower in order from small to large. The aim is to move all the discs on the left pole to the right pole, provided that only one disc can be moved at a time, and the large plate is not allowed to be placed on top of the disc.

* Problem analysis and algorithm design

This is a famous question, almost all textbooks have this problem. Because the condition is to move only one disk at a time, and do not allow the market to be placed on the small disk, so the number of 64 disk moves is:

18,446,744,073,709,551,615

This is an astronomical figure, if each microsecond may calculate (does not output) a move, then it will take almost 1 million years. We can only find the solution to the problem and solve the Hanoi of the smaller n value, but it is very difficult to solve the 64-layer Hanoi with the computer.

Analyze the problem and find the correct algorithm for moving the plate.

First consider the plate under a pole rather than the top plate on the pole, and the task becomes:

* Move the top 63 plates to the B bar;

* Move the remaining plate on a bar to the C pole;

* Move all plates on the B bar to the C pole.

To continue this process, you have to finish moving 63 plates, 62 plates, 61 plates .... of work.

To describe the algorithm more clearly, you can define a function Movedisc (n,a,b,c). The function is to move n plates from a pole to the B bar with the help of a C rod. The work of moving n plates in this way can be done as follows:

1) Movedisc (N-1,A,C,B);

2 move a plate from A to B;

3) Movedisc (n-1,c,b,a);

Repeat the process until all the plates are moved in place.

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.