Hanoi Hanoi Tower

Source: Internet
Author: User

An ancient Hindu legend: in the center of the world, in the heart of Sheng Miao, a yellow copper plate was inserted with three stone needles. In the creation of the world, The Hindu god Brahma, on one of the needles, dressed from the bottom to the top 64 pieces of gold, which is called the Hanoi (Hanoi Tower). No matter the day or night, there is always a monk in accordance with the following rules to move these gold pieces: one at a time, no matter where the needle, the small pieces must be on top of the large.

The monks predicted that the end of the world would come when all the gold pieces were moved from the golden sheet of Brahma to another, and that the Vatican, the temples and the sentient beings would perish ...

The story does not say much, Hanoi is a typical application of recursive thinking, on the code:

1#include <stdio.h>2 3 //move n pieces of gold, with Y, from X to Z4 voidMoveintNCharXCharYCharz)5 {6     if(1==N)7printf"%c\n,%c", x,z);8     Else9     {TenMove (n1, X, z, y);//n-1 a gold sheet from X, with Z, move to y Oneprintf"%c\n,%c", x,z);//move the nth gold sheet from X to Z AMove (n1, y, x, z);//move n-1 a gold sheet from Y, x, to Z -     } - } the  - intMain () - { -     intN; +printf"Please enter the number of layers for Hanoi:"); -scanf"%d",&n); +Move (N,'X','Y','Z'); A  at     return 0; -}

Finally, consider the relationship between the number of steps and the number of gold slices moved:

Each additional piece of gold, it moves the number of steps is equal to the original step twice times plus 1. The recursive formula is: f (n+1) = 2*f (n) + 1, it is not difficult to get f (n) = 2^n-1.

For example, 3 pieces of gold steps for 7, then 4 pieces of gold steps for 2*7+1=15 step, 5 pieces of gold steps for 2*15+1=31 step.

At this point, you can calculate the number of pieces of gold is 64 o'clock, moving the number of steps: 18446744073709551615 steps. Suppose 1 seconds to move, the time required is: 1.,844,674,407,370,96e,+19 seconds, about 584.5 billion years, to the time the universe is not ...

Hanoi 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.