Initial knowledge of Hanoi Tower

Source: Internet
Author: User

Hanoi

Hanoi (Tower of Hanoi) originated from the Hindu legend, the great Brahma created the world when the creation of three gold steel pillars, one of the pillars from the bottom up 64 pieces of gold disc. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

--Quote from Wikipedia

If the Hanoi legend of the three pillars are named in the English alphabet A,b,c, of which only a pillar placed n disk (1<=n<=100000), to transfer all the discs on a pillar to the C pillar, ask the minimum number of times to move the disc.

The rules for moving disks are as follows:

    1. You can only move one disc at a time
    2. A large diameter disc must be placed on a disc with a small diameter.
Recursive solution

The Hanoi tower problem is solved by simple recursion, the code is simple and easy to understand. In fact, the number of moving times of the Hanoi Tower problem is regular and can be found, through recursive code to find the corresponding law, and through the mathematical method to obtain results efficiency is the highest.

    • When N=1, a pillar has only one disc and is moved directly to the C-pillar
    • When n>1, according to rules 1 and 2, the A pillar n-1 a disk to the B pillar, and then a disk of the rest of a is moved to C, and then the B is temporarily placed on a n-1 disk to C

Recursive solution is the process of continuously reducing the size of the problem, moving the B-pillar n-1 disk to C does not repeat the above two points of the process. The recursive solution is shown in C code 3-1.

voidHanoi (intNCharACharBCharc) {    if(n = =1) {Move (A, c); }    Else{Hanoi (n-1, A, C, b);        / * Move a pillar n-1 a disc to column B * /      Move (A, c);        / * Move the rest of a disc to c*/ Hanoi (n-1, B, A, c); / * Then move the n-1 disc that is temporarily on B to c*/ }}voidMove (CharACharb) {printf ("Move 1 Disk:%c--------->%c\n", A, b);}

Figure 3-1 Hanoi recursive solution code

3-1 of the code can derive the recursive relationship of the number of Hanoi moving discs:

    • Hanoi (n) = 1, n = 1;
    • Hanoi (n) = 2 * Hanoi (n-1) + 1, n>1;

B (n) = Hanoi (n) +1, it can be concluded that B (n) is a 2 base, the male ratio of 2 geometric series. This is what you get,

B (n) = 2n, n>0

Hanoi (n) = 2n-1, n>0

Hanoi to increase the constraint conditions

Q: If you add a restriction, the disc can only move between adjacent columns , and how to solve it? Suppose that A, B, C side-by, b in the middle, that is, a, C is not adjacent, a disk on a to move to C must first move to B, and then moved to C.

From TOJ 3270 Strange Hanoi Tower

Similarly, using recursive solution, according to the original rules and new constraints, the law of the number of moving discs is deduced.

    • When N=1, a pillar has only one disc, moving first to B, then to C
    • When n>1, the n-1 disk of column A is moved to the C pillar by the B-pillar, and then the remaining disk of a pillar is moved to B; Then the C-pillar n-1 disk is moved through the B-column to A; Then move the only disc of the B pillar to C, and finally move the n-1 disk of the A pillar through B to C.

Recursive code 3-2 is shown.

voidHanoi (intNCharACharBCharc) {    if(n==1{ //a pillar has only one disc, move to B first, then move to C Move (A, b);    Move (b, c); }    Else{Hanoi (n-1, A, B, c);//First move the n-1 disc of a column through the B-column to the C- pillarMove (A, b);the remaining disc of the//a pillar is moved to B.Hanoi (n1, C, B, a);//Move C-pillar n-1 disc through B-column to aMove (b, c);//To move the only disc of column B to CHanoi (n1, A, B, c);//Move the n-1 disc of a pillar through B to C    }}voidMove (CharACharb) {printf ("%c --%c\n", A, b);}

Figure 3-2 Hanoi recursive code for adding constraints

A recursive relationship between the number of discs and the number of discs is available from Figure 3-2,

    • When N=1, Hanoi (n) = 2;
    • When N>1, Hanoi (n) = 3 * Hanoi (n-1) + 2;

Finally, the following is obtained:

Hanoi (n) = 3n-1, n>0

The result of this problem can be very large, the output of the result is mod 1,000,000,007.

The output of the topic prompted me to wonder, the result is very big and the MoD this number is related. Baidu a bit, seemingly with what fee Ma Xiao theorem related. But I still do not understand, first to keep the research and study.

Initial knowledge of Hanoi Tower

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.