"Data structure and algorithm" recursive Hanoi

Source: Internet
Author: User
Hanoi
Hanoi is a mathematical problem formed according to a legend (about Hanoi):
There are three Poles a,b,c. A rod has N (n>1) perforated discs, the size of the disk from bottom to top in turn smaller. The following rules are required to move all discs to the C-bar:
Only one disk can be moved at a time;
The market cannot be stacked on a small plate.
Tip: The disc can be temporarily placed in the B-bar, or the disc removed from the A will be moved back to the A-bar, but must follow the above two rules.

Recursive Hanoi
Problem-Solving ideas: You can simplify the question to 2 plates, such as: A on two plates, B and C is empty. If you want to move all two plates of a to C, you need to go through the following steps:
1.A move a plate to B
2.A move a plate to C
3.B move a plate to C
Now that you have moved all the plates to C, you can actually think of the plates on the bottom of the chassis as a whole, which simplifies the 3-step process, and then uses the recursive repetition of the 3 steps to complete the displacement of all the plates.
Use a recursive Chen Hanno tower and print the changes and count steps for each step in real time.

"Data structure and algorithm" recursive Hanoi

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.