C # algorithm overview-1-Hanoi

Source: Internet
Author: User

If you are looking for a job, you can use the C # language for the questions in the classical algorithm. Please do not spray.

Hanoi Tower

DescriptionTowersof 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, which was supported by three diamond rods (Pag, at the beginning, God placed 64 Gold Disk Disc arranged from top to bottom in a descending order on the first stick, and ordered the monks to move all the gold disks from the first stone stick to the third one, in addition, the principle of carrying large dishes under small dishes is observed. If only one plate is moved every day, the tower will be damaged when all the dishes are moved, that is, when the end of the world is approaching.

SolutionIf 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 there are more than two disks, it is very easy to cover the third and lower plates. Each time we 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: 264-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.
Namespace HanoiProgram {class Program {static long count = 0; // count the number of moves. static void move (char x, char y) // The handling track of this step {Console. writeLine ("{0} ---> {1}", x, y);} static void hanoi (int n, char one, char two, char three) {if (n = 1) {count + = 2; move (one, two); move (two, three);} else {count + = 2; hanoi (n-1, one, two, three); move (one, two); hanoi (n-1, three, two, one); move (two, three ); hanoi (n -1, one, two, three) ;}} static void Main (string [] args) {Console. writeLine ("several dishes need to be moved"); int n = int. parse (Console. readLine (); hanoi (n, 'A', 'B', 'C'); Console. writeLine ("relocation completion speed; A total of {0} migrations were performed. ", Count); Console. Read ();}}}


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.