Hanoi (hannoo) tower Problems

Source: Internet
Author: User
Hanoi (hannoo) tower problems. This is a classical mathematical problem and a typical example of solving problems using recursive methods. The problem is: in ancient times, there was a fan tower with three seats A, B, and C in it. At the beginning, there were 64 dishes on the, which were of varying sizes and were big, small. An Old Monk wants to move the 64 dishes from seat a to seat C, but only one disk can be moved at a time. During the movement, the tray is always kept under the three seats, and the small tray is on. A block B can be referenced during the movement process, requiring the compiler to output the moving steps.
It is unlikely that the reader will directly write down every specific step of the mobile plate. Please try to move the five plates from seat a to seat C according to the above rule. Can you write down each step directly?
The old monk will naturally think like this: adding another monk can have a way to move 63 dishes from one seat to another. The problem is solved. At this time, the old monk only needs to do this:
(1) Order the 2nd monks to move 63 dishes from seat a to seat B;
(2) move one plate (tHe bottommost and largest plate) from seat a to seat C;
(3) then, order the 2nd monks to move 63 dishes from seat B to seat C.
So far, all the tasks have been completed. This is the recursive method. However, one problem is not solved: How can 2nd monks move 63 plates from seat a to seat B?
To move 63 dishes from seat a to seat B, 2nd monks thought: if someone could move 62 dishes from one seat to another, I can move 63 dishes from seat a to seat B. He did this:
(1) Order the 3rd monks to move 62 dishes from seat a to seat C;
(2) move one plate from seat a to seat B;
(3) then, order the 3rd monks to move 62 dishes from seat C to seat B.
Perform another recursion. So "layers don't put" Until then we found the 63rd monk and asked him to move the two plates from one to the other. Then, the problem was almost solved. Finally, I found the 64th monk and asked him to move one plate from one to another. At this point, all the work has been completed and can be executed.
It can be seen that the end condition of recursion is that the last monk only needs to move one plate; otherwise, recursion will continue.
It should be noted that only after the task of 64th monks is completed can the task of 63rd monks be completed. Only 2nd to 64th monks can complete their tasks. It is just a typical recursive problem.
For ease of understanding, first analyze the process of A's three plates moving to seat C: [Here is the focus :]

(1) move two plates on seat a to seat B (using C );
(2) move one plate on seat a to seat C;
(3) Move the two plates on seat B to seat C (using ).

Steps (2) can be directly implemented. STEP (1) can be decomposed into the following recursive methods:
1.1 move one plate on A from A to C;
1.2 move one plate on A from A to B;
1.3 move one plate on C from C to B.
Part (3) can be divided:
3.1 move one plate from B to;
3.2 move one plate from B to C;
3.3 move one plate on A from A to C.
After combining the preceding steps, we can move the three plates as follows:
A → C, A → B, C → B, A → C, B → A, B → C, A → C.
A total of seven steps have been taken. This can be introduced: Moving n plates to endure 2n-1 steps. For example, you can move four dishes in 15 steps, move five dishes in 31 steps, and move 64 dishes in step 1.
The above analysis shows that moving n plates from seat a to seat C can be divided into the following three steps:

(1) Move n-1 disks on A to B first with Block C;
(2) Move the remaining disk on seat a to seat C;
(3) Move n-1 disks from seat B to seat C using seat.

Steps (1) and (3) above move the n-1 disk from one seat to another. The method is the same, but the seat name is different. Steps (1) and (3) can be expressed:
Move n-1 disks on the "one \" seat to the "two" seat (with the help of the "three" seat ). In step (1) and step (3), the Mappings of one, two, three and A, B, and C are different. For step (1), one corresponds to a, two corresponds to B, and three corresponds to C. For step (3), one corresponds to B, two corresponds to C, three corresponds to.

Therefore, the preceding three steps can be divided into two types of operations:
(1) Move n-1 disks from one seat to another (n> 1 ). This is the work done by the monk and the Monk. It is a recursive process, that is, the monk will lay down the task layer by layer until 64th monks.
(2) move one plate from one seat to another. This is a job done by Dahe and Shang himself.
Write the program below. Use two functions to implement the above two types of operations, and use the hanio function to implement the above 1st types of operations (that is, to simulate the task of Xiao monk ), use the move function to implement the above 2nd types of operations (simulate Yamato and manually move the disk), and call the function hanio (n, one, two, three) the process of moving n plates from the \ "one" seat to the \ "three" seat (with the help of the "two" seat ). The function calls move (x, y) to move one plate from seat X to seat y. X and Y represent A, B, and C, respectively, according to different situations.
The procedure is as follows:

#include<iostream.h> 
void main() 

void hanoi(int n,char one,char two,char three); 
int m; 
cout<<\"input the number of diskes:\

  • Previous: static member functions call non-static member variables
  • Next article: BAT deletes empty directories and subdirectories
  • 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.