012 keep it up)

Source: Internet
Author: User

Tower of Hanoi: There are three adjacent columns, labeled as A, B, C, and a, stacked with N disks of different sizes in pyramid shape from bottom to top, how should I move all the plates one by one to Column B, and each time I move the same column, there cannot be big plates above the small plate?

The tower of Hanoi is a very classic problem. It should be mentioned in recursion. If we do not have the prior knowledge of recursion to answer this question directly, we often feel that we do not know how to start. Recursion can solve this problem very beautifully.
The key to recursion is to define a function first, so you don't have to rush to implement it, but you need to clarify its functions.
For the tower of Hanoi Problem, we define the following function prototype:
Void Hanoi (int n, char SRC, char Bri, char DST );
We should not care about how it is implemented, but clarify that its functions are:
Move n discs from the column SRC to the column DST, with the help of the column BRI (BRIDGE ).
Note: The numbers of N disks from top to bottom are 1 to n, indicating that the disks are small to large.
During the process of moving, the large disc is not allowed to be placed on the small disc.
Okay. Now that recursion is required, we still use this function in this function. That is to say, the Hanoi operation will be used to complete the steps in this task, but the parameters may be different. We define a group of tuples to indicate the state of the three pillars: (the disc on SRC, the disc on Bri, And the disc on DST) the initial state is :( 1 ~ N, 0, 0) indicates that there are 1 to n disks on SRC, and there are no disks on the other two columns. The target status is: (0, 0, 1 ~ N) indicates that there are 1 to n disks on DST, and there are no disks on the other two columns. Since the Largest Disc N is placed at the bottom of DST and the large disc cannot be placed on the small disc, such an intermediate state must exist: (n, 1 ~ N-1, 0) to move the Largest Disc n to the bottom of DST. At this time, someone will ask, how do you think of this intermediate state instead of others? Good question. Because the tools (available functions) at hand are only Hanoi, I naturally want to create a scenario where this function can be used, rather than other scenarios.

The initial status is: (1 ~ N, 0, 0)
The intermediate status is: (n, 1 ~ N-1, 0)
From the initial state to the intermediate state, the Operation Hanoi (n-1, SRC, DST, Bri) will be available. That is, the n-1 disc is moved from SRC to Bri, and the column DST can be used in the middle.

The next step is to move the disc N from SRC to DST, which can be output directly:

Cout <"Move disk" <n <"from" <SRC <"to" <DST <Endl;
The status after this operation is:
(0, 1 ~ N-1, n)
Then, use the Hanoi function to move n-1 disks from Bri to DST. The column SRC, Hanoi (n-1, Bri, SRC, DST) can be used in the middle, and the final state is obtained after the operation:
(0, 0, 1 ~ N)
These operations are combined with three lines of code:

Hanoi (n-1, SRC, DST, Bri );
Cout <"Move disk" <n <"from" <SRC <"to" <DST <Endl;
Hanoi (n-1, Bri, SRC, DST );
Finally, we need to recursively stop conditions. When does recursion end? When N is equal to 1, when there is only one disc, you can directly move it from SRC to DST:
If (n = 1)

{
Cout <"Move disk" <n <"from" <SRC <"to" <DST <Endl;
}

void hanoi(int vN, char vSrc, char vBriage, char vDest){if (vN == 1){std::cout << "move the dist "<< vN << " from " << vSrc << " to " << vDest << std::endl;return;}hanoi(vN-1, vSrc, vDest, vBriage);std::cout << "move the dist "<< vN << " from " << vSrc << " to " << vDest << std::endl;hanoi(vN-1, vBriage, vSrc, vDest);}


012 keep it up)

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.