Recursive algorithm and analysis of Hanoi

Source: Internet
Author: User

From left to right a B C pillar large plate under the small plate on the use of B-pillar to move all the plates from the A-column to the C-pillar, there is only one principle: the big plate only under the small plate.

If there are 3 plates, the larger the smaller, the lower the more on the top, from the above to the plate in order numbered 1 (small), 2 (Medium), 3 (Large), the following principle of the resolution refers to the number here.

Played this game as a child, basically play to the 7th, the 8th is very impatient to play, and the operation of the action are almost the same feeling bored. Later learning programming, recognize recursion, and recursive solution Hanoi algorithm is my only simple sorting algorithm after learning the first algorithm.

As for recursion, it is simply that the method internally calls itself, and there must be an end point. If you understand the method call stack, it is very easy to understand the method of the call process, that is, starting from the main thread to call the method to continue to stack and stack operations. The method is called to push the method into the stack, the end of the method is the process of the method out of the stack, thus guaranteeing the sequential flow of the method call. If the tracing recursive call condition will be found as well, finally it must be that this method finally pops out of the stack back to the main thread and ends.

Features of the stack: Advanced post-out. For example a method A calls itself, I use the number to distinguish the process of the stack:

A-A (1)-A (2)-A (3)

At a (3) when a certain condition can be exited, back to a (2), a (2) end back to a (1), and then back to a, the stack process:

A (3)-A (2)-A (1)-A

For recursion, there is an image of the understanding that I was a child at home there is a cupboard, the cabinet ends are glass, the head into the cabinet to see a mirror, will see mirrors and mirrors, and mirrors, but the characteristics of the mirror is not the end of the mirror, as long as the eyes have been able to see the bottom.

After understanding the recursion, then look back to see how to solve the problem of Hanoi in a recursive way.

Case 1-Assuming that there is only one plate, the number of plates n=1

There is only one step to move the 1th plate from a to C, so I can describe this step for the sake of comparison:

Step plate number move from pillar to pillar

1 1 A C

Case 2-If there are two plates, the number of plates N = 2

Step plate number move from pillar to pillar

1 1 A B

2 2 A C

3 1 B C

Case 3-If there are three plates, the number of plates N = 3

Step plate number move from pillar to pillar

1 1 A C

2 2 A B

3 1 C B

4 3 A C

5 1 B A

6 2 B C

7 1 A C

How to find out the regularity of plate movement?

The most important thing we need to do is to always move the bottom plate from a to C.

Look at the movement of the above from 1 plates to 3 plates, in the moving record, when the plate number and the number of plates are the same, their steps are moved from a to C (see bold part), the other steps are equivalent.

Observe the 第1-3 and 第5-7 steps in the 3rd case

The 第1-3 step is to move from a to B if we take B as the end, then the 第1-3 step here is exactly the same as the three steps in the 2nd case, all moving through a pillar, and the 2nd case is followed by parentheses to indicate

1 1 A C (A-B)

2 2 A B (A-C)

3 1 C B (b-C)

Summary: Turn plate B into C.

第5-7 step to move from B to C if we take C as the end, then the 5-7 steps here are the same as above, and the three steps in the 2nd case are exactly the same. Compared to the 2nd case:

5 1 B A (A-B)

6 2 B C (A-> C)

7 1 A C (B-C)

Summary: Turn plate B into a

According to this demonstration, several rules can be clarified:

1. When there is only one plate, only one action moves from a to C that ends.

2. When there are n plates, the middle action is moved from A to C, so that the bottom of the nth plate move is complete

3. The intermediate action can be considered as: moving from A to B

4. The middle action can be considered as: moving from B to C

2,3,4 can be expressed as

1 1 A B

2 2 A C

3 1 B C

First, play

1. There are three Poles a,b,c. There are several plates on the A-pole.
2. Each time you move a plate, the small one can only be stacked on top of the large AA
3. Move all the plates from the A to the C-bar

Second, answer ideas

This is the application of a typical recursive scenario

1 //N-the number of discs to move2 //A- moving lever, B-lever, C-Target lever3  Public StaticHanoiintn,string A, String b,string c) {4       if(n = = 1) {5System.out.println (A + "--->" +c);6}Else {7Hanoi (n-1, a,c,b);8System.out.println (A + "--->" +c);9Hanoi (n-1, b,a,c);Ten       }     One}

1  Public classHanoitest {2   3     Static intStep = 0; 4     /** 5      * @paramargs6      */  7      Public Static voidMain (string[] args) {8Haniosort (3, "A", "B", "C"); 9     }  Ten        One     /**  A * Recursive function, used to traverse Hanoi steps -      */   -      Public Static voidHaniosort (intnum, string A, string B, string c) {   the         if(num = = 1){   - Move (NUM,A,C);  -}Else{   -Haniosort (num-1, A, C, b);  + Move (NUM,A,C);  -Haniosort (num-1, B, A, c);  +         }   A     }   at      Public Static voidMoveintnum, String a,string b) {   -Step + + ;  -System.out.println ("+step+" step, plate "+num+" moved from "+a+" Tower to "+b+" Tower/n ");  -     }   -}

Recursive algorithm and analysis of 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.