Today, I finally understood the tower. I used to know little about it.

Source: Internet
Author: User
Tower of Hanoi

The origins of the tower
Pandata is a toy derived from Indian mythology.
When God created the world, he made three diamond pillars and placed 64 gold disks in order from bottom to bottom.
God ordered the Brahman to re-place the disc from below in order of size on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars at a time.
Some languages say that when this is done, the universe will be destroyed in a flash. Some people believe that Brahman is still moving the disc for a moment.
Tower of life and cosmic life
If it takes one second to move a disc, when will the universe be destroyed when all the 64 discs fall together?
Let's consider how many times the 64 disks need to be moved. One time is of course one time, two times is three times, and three times is used seven times.
So let's think logically.
You can move up to four disks in four hours.
So far, seven times have been used.
Next, if you use it once, you need to use it 7 times when you put three more disks on it (the number of times required to re-place the three disks together ).
Therefore, when there are four
"The number of times three disks are stacked together" + 1 + "the number of times three disks need to be stacked together"
= 2x "Number of times when three disks are stacked together" + 1
= 15 times.
So, n is
2x "(n-1) Number of times the disc is stacked together again" + 1 time.
Since one time is one time, the result N is (the Npower of 2 minus 1.
When one disc is created, the power of 2 is reduced by 1.
When two disks exist, the power of 2 is reduced by 1.
When three disks exist, the power of 2 is reduced by 1 to the power of 3.
When there are four disks, the power of 2 is reduced by 1 to the power of 4.
When there are five disks, the power of 2 is reduced by 1 to the power of 5.
........
When N disks exist, the Npower of 2 is reduced by 1.
That is to say, when n is 64, it is (the power of 2 is reduced by 1.
Therefore, it takes one second to move a disc,
The lifetime of the universe = the power 64 of 2 minus 1 (seconds)
It takes about 580 billion years for a year = 60 seconds x 60 Minutes x 24 hours X days.
It is said that the age of the universe is about 15 billion years.
The quanta issue has a high research value in the field of mathematics,
And it is still being studied by some mathematicians,
It is also a puzzle game that we like to play,

It can help develop intelligence and stimulate our thinking.

RecursionProgramSolution (the results of move1 and move2 are exactly the same ):

Stringbuilder Sb;
Long Count;

private void button3_click (Object sender, eventargs e)
{< br> Sb = new stringbuilder ();
COUNT = 0;
int n = int. parse (this. textbox2.text);
This. move2 (n, "A", "B", "C");
This. textbox1.text = count. tostring () + "," + (math. pow (2, n)-1 ). tostring () + "\ r \ n" + sb. tostring ();
}

Private void button4_click (Object sender, eventargs E)
{
SB = new stringbuilder ();
Count = 0;
Int n = int. parse (this. textbox2.text );
This. move1 (n, "A", "B", "C ");
This. textbox1.text = count. tostring () + "," + (math. pow (2, n)-1 ). tostring () + "\ r \ n" + sb. tostring ();
}

// Move n gold Slices from A to C through B
Private void move1 (int n, string source, string passed, string target)
{
++ Count;
If (n = 1)
{
// Print N from A to C
SB. append (source + "-" + target + "\ r \ n ");
}
Else
{
Move1 (n-1, source, target, after); // move the N-1 gold from A to B through C,
// Print n to move from A to C, because all gold clips above have been removed from the previous step.
SB. append (source + "-" + target + "\ r \ n ");
Move1 (n-1, going through, source, target); // solves the problem of N-1 gold tablets passing through a to C from B, because at this time N-1 gold tablets on B
}
}

Private void move2 (int n, string source, string passed, string target)
{
If (n <1)
Return;

If (n = 1)
{
// Print N from A to C
SB. append (source + "-" + target + "\ r \ n ");
++ Count;
}
Else
{
Move2 (n-1, source, target, after); // move the N-1 gold from A to B through C,
// Print n to move from A to C, because all gold clips above have been removed from the previous step.
SB. append (source + "-" + target + "\ r \ n ");
++ Count;
Move2 (n-2, pass, target, source );
SB. append (after + "-" + target + "\ r \ n ");
++ Count;
Move2 (n-2, source, pass, target );
}
}

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.