Write Hanoi in C + +

Source: Internet
Author: User

Hanoi (RTHK: River Tower) is a mathematical problem formed according to a legend.
There are three Poles a,b,c. A rod has N (n>1) perforated discs, the size of the disk from bottom to top in turn smaller. The following rules are required to move all discs to the C-bar:
-Only one disc can be moved at a time
-The large plate cannot be stacked on a small plate

The first person to have this problem was the French scientist, Edward. Cass
It is said that there are three pillars in a monastery in India, and a string of 64 gold plates. The monk partners in the monastery moved these plates in accordance with an ancient prophecy; the world, when these plates were moved, was
Will perish. This is known as the Tower of the Brahma Temple (Tower of Brahma puzzle). But I do not know that it is the legend of the self-created, or he was inspired by others.
If it is true, the monk partners need 264-1 steps to accomplish this task; if they can complete a plate movement per second, it will take 5,849 years to complete. The whole universe is now 137 years old.

This legend has several changes: monasteries converted to monasteries, monks partners to monks and so on. The temple's ground point says colorful repaying, one of which is located in the river in Vietnam, is named "River Tower". There is also a background setting for "The golden Plate is created during creation" and "the monk partners to move a plate every day".
There is actually a building of "pagoda" in Buddhism, and some of the pagodas are built in accordance with the above rules. One of the "river Towers" may have been transferred to Europe by the middle-south half of the island during the colonial period.
If you take n=64, you need to move at least 264-1 times. That is, if you can move a disk one second, it will take 584.942 billion years. Now, according to the theory of the Big Bang, the universe is only 13.7 billion years old.


So first move the N-1 block on tower A to Tower B, and then move the remaining plate of tower A to C, and finally move the B-Tower's N-1 to C.
Each time you move more than one disk, the above algorithm is used again to move.


void Hannoi (const int & n,const Char & A,const Char & B,const Char & c)
{
if (n = = 1)
{
cout << "Move disk" << n << "from" << a << "to" << C << Endl;

}
Else
{
Hannoi (N-1, A, C, b);
cout << "Move disk" << n << "from" << a << "to" << C << Endl;
Hannoi (n-1, B, A, c);
}
}

int main ()
{
int n = 0;
cout<< "Enter the N:";
CIN >> N;
Hannoi (n, ' A ', ' B ', ' C ');
return 0;
}

Write Hanoi in C + +

Related Article

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.