"Hanruata" in the algorithm series"

Source: Internet
Author: User
I. hanruata

1. Description:

Towers of Hanoi is French M. claus (Lucas) was taken from Thailand to France in 1883. Hanoi is the capital of Vietnam's North Vietnam, now Ho Chi Minh City. In 1883, French mathematician Edouard Lucas mentioned this story, it is said that at the time of the founding century, Benares had a boro church backed by three diamond bars (PAG, at the beginning, God placed 64 gold disks (disc) arranged from top to bottom in ascending order on the first stick ), and ordered the monks to move all the gold disks from the first stone rod to the third stone rod, and observe the principle that the big plate is under the Small Plate during the handling process. If only one plate is moved every day, when the plate is fully moved, the tower will be damaged, that is, the end of the world.

2. Solution

If the column is marked as ABC, it will be moved from A to C. When there is only one plate, it will be moved directly to C. When there are two plates, B will be treated as the auxiliary column. If the disk

If there are more than two, it is easy to cover the third and lower plates. Each time you handle two plates, that is: a-> B, A-> C, B-> C, and the hidden part is actually the progressive processing of the program. In fact, if there are n plates, the number of moves required is 2 ^ n-1, so when the number of disks is 64, the required number is: 2 ^ 64-1 = 18446744073709551615 is 5.05390248594782e + 16 years, that is, about 5000 century. If there is no concept of this number, it is assumed that a plate is moved every second, it is also about 585 billion years old.


Void Hanoi (int n, char a, char B, char c ){

If (n = 1 ){

Printf ("Move sheet % d from % C to % C \ n", n, a, c );}

Else {

Hanoi (n-1, A, C, B );

Printf ("Move sheet % d from % C to % C \ n", n, a, c );

Hanoi (n-1, B, A, C );

}

}

Int main (){

While (1 ){

Int N;

Printf ("Enter the number of disks :");

Scanf ("% d", & N );

Hanoi (n, 'A', 'B', 'C ');

}

Return 0;

}


"Hanruata" in the algorithm series"

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.