Understanding and realization of Hanoi recursive algorithm

Source: Internet
Author: User

Hanoi: (Hanoi) is a kind of toy,! [Write a description of the picture here] (http://img.blog.csdn.net/20150430225337439)
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.
Problem Understanding and Description:
1. The understanding and description of the problem
The formal representation of the problem is:
Input: Number of discs n,3--source rod A, transition rod B, and target Rod C.
Output: The minimum sequence of steps that the disc moves from the source rod to the target rod process.
2. Pseudo-Code of the algorithm:

A, B, C)1if n=12   A,"?", C3       return4 HANOI(n-1,A, C, B)5A,"?", C6 HANOI(n-1A, C)

3. Run time of the algorithm:
Run time for the N,hanoi process for the number of disks

4 Algorithm Understanding:
Understanding One: (Ref.: http://blog.csdn.net/yafei450225664/article/details/8647908)

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
Understanding Two: (Reference: http://blog.csdn.net/leo115/article/details/7991734)
An American scholar has discovered a surprisingly simple algorithm that can be achieved by taking two steps: First, arrange the three tables in sequence, form a ring, and then start moving the plates on a, and place them clockwise in the order of a B C:
If n is odd, the order in which the discs are moved is a->c->b->a->c->b->a ... That is, the interval is two steps moving. The n here represents the number of layers on the plate, for example, the 3-story Hanoi is the order of the 1th and 3 plates moving from bottom to top.
If n is even, the order of the discs to move is a->b->c->a->b->c->a .... That is, the interval is shifted by one step. Explanation of n the same as the second plate move a->b->c.
5. Code implementation (c):

/*************hanoi.c********************/#include <stdlib.h> #include <stdio.h> #include"Hanoi.h"/************* Find the X-Rod top plate number ********** input parameters: Current[i] Record the number of the number I is located in the bar X; the number of the bar output parameter: X-BAR Top plate number **************************** ************/intPicktopdisk (Char* Current,Charx) {inti =0; while(Current[i]! = x) i++;returni;}/************* Hanoi ********** input parameter: current[i] record the pole number of the number i is on N: Number of disks a,b,c: number of Poles ***************************** ***********/voidHanoiChar* Current,intNCharACharBCharC) {Static intcout =0;The //static type variable saves the changed value when the function is called multiple times, and the initialization operation is done only once    inti =0;if(n==1) {i = Picktopdisk (current, A);        Current[i] = C;        cout++; printf"Move%d disk%d:%c->%c\n", cout, i +1, A, C);return; } Hanoi (current, N-1, A, C, B); current[n-1] = C;    cout++; printf"Move%d disk%d:%c->%c\n", cout, N, A, C); Hanoi (current, N-1, B, A, C);}
/****************hanio.h********** **/  #ifndef _hanoi_h   #define _hanoi_ H   #ifdef __cplusplus  extern  "C"  { Span class= "Hljs-preprocessor" > #endif//__cplusplus  int  picktopdisk (char  * Current, char  x); void  Hanoi (char  * Current, int  N, char  A, char  B, char  C);  #ifdef __cplusplus }  #endif   #endif   
/**************main.c************************/"hanoi.h"int main(intchar** argv){    char‘A‘‘A‘‘A‘‘A‘ };    char‘A‘‘B‘‘C‘;    4, A, B, C);    system("pause");    return EXIT_SUCCESS;}

6 Operation Result:

Reference: "Algorithmic design, analysis and implementation: C, C + + and Java" Xu Zisan

Hanoi recursive algorithm understanding and implementation

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.