HANOI tower (C)

Source: Internet
Author: User

Problem description: in ancient times, there was a fan tower with three seats in the tower, A, B, and C. At the beginning, there were 64 disks in the seat, with sizes ranging from large to large, lower. An Old Monk wants to move the 64 dishes from seat a to seat C (), but only one disk can be moved at a time. During the movement, the tray is always kept under the three seats, and the small tray is on. In a mobile location, you can use block B to print the steps for moving.

 

Reverse reasoning: 1. if a monk can move the 63 dishes to seat B first, and the second monk moves the largest one to C, then the third monk moves 63 dishes to seat C. Now the work is complete.

2. The problem is how to move 63 dishes to seat B. In the same way, move 62 dishes to seat C first.

, Move the first 63rd plates to seat B, and finally 62 to seat B.

3 ...... And so on;

4. From the above analysis, we can see that only when the monk behind the plate is finished, can the former monk complete the task. Let's use the data structure of Stack: data is processed only at one end, and is advanced and later. Therefore, recursive processing is correct.

 
(Tower chart)

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace hanoiprogram
{
Class Program
{
Static int COUNT = 0; // count, total number of times the plate was moved;
Static void printmove (char X, char y)
{
Console. writeline ("{0} --> {1}", x, y );

}

Static void Hanoi (int n, char one, char two, char Three)
{
If (n = 1)
{
++ Count;
Printmove (one, three );

}
Else
{
++ Count;
// Use the middle tower to move the n-th plate (n-1) to another disk;
Hanoi (n-1, one, three, two );
// Print the steps for moving the nth disk to the target tower;
Printmove (one, three );
// Use the middle tower to move the (n-1) plate on the n-th plate to the tower where the n-th plate is now located;

Hanoi (n-1, two, one, three );
}

}
Static void main (string [] ARGs)
{
Console. writeline ("starting to move 64 dishes ..");
Hanoi (4, '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.