Hanoi of algorithm series)

Source: Internet
Author: User

Author: I _dovelemon

Source: csdn

Date: 2014/11 4

Topic: recursion, Hanoi



Introduction

In the world of algorithms, there is a powerful tool such as recursion. Many algorithms can be implemented using recursion, which is very simple and easy to understand. Today, we will explain a problem solved by recursion. The famous Tower of Hanoi problem can be solved using recursive algorithms. Let's take a look at what the Quanta problem is:

The quanta issue was first published by a French mathematician, Lucas in 1890. The version was like this. When 64 discs were removed from quanta, the end of the world would be around. If the priest moves a disc every minute, estimate how many years it will take to remove all the discs? (The rule is as follows: Only one plate can be moved at a time, and each move must ensure that the above plate is smaller than the below plate)

The following is an illustration of the qingta problem:

That is to say, we need to move the plate on the first pillar to the third pillar under the conditions that meet the above rules.



Solution

We can solve this problem through recursive thinking. First, we need to make it clear that we want to move the disc on the first pillar to the third pillar, then we need to use the second pillar, move N-1 1 disc on the first column (if a total of N disks) to the second column. Then we can put the Largest Disc on the first pillar to the third pillar. The rest is to move all the data on the second pillar to the third pillar. This recursion completes the entire movement.

From the above text description, we will summarize three steps:

1. Move the n-1 disc on the first column to the second column.

2. Move the only disc on the first pillar to the third pillar.

3. Move the disc on the second pillar to the third pillar.

In the three steps, the second step is obviously a single non-recursive step, so it is a direct operation step. How can we implement the first and third steps?

In this case, the above three steps are the total number of methods for moving the entire N, and the above method can be successfully moved. For n-1, we can also use this method. The difference is that the positional relationship between the three columns has changed. If we use Hanoi (n, a, B, c), we move the n columns on A to C with B. So for n-1, we also use the above three steps, that is, I want to move the n-1 disc on the column to the B column with the help of C, in this case, we can use Hanoi (n-1, A, C, B) to represent the purpose of the last operation in N. This is because we use the replacement idea to change the positional relationship between the three columns so that we can use C to move the disc on A to B. Is so constantly changing position, when we in the N-2 recursion, because of the position change, and become Hanoi (n-2, A, B, C), so repeatedly.

Of course, the above steps only summarize the recursion of the first step in the summary, that is, after the above recursive operation is completed, we just completed the induction summary about moving n-1 plates to the second pillar. At the same time, we also need to note that in the recursion process, we need a condition to end the recursion. This end condition is when there is only one disc on the column (note, here, A is not the first fixed column in the figure. A may be any column because they need to be constantly changed during recursion ), we can place the disc directly on C (C is also explained as a), and the recursive operation ends.

Well, when one step is complete, there is only one disc on the first pillar, and we put the disc directly on the third pillar. In this step, no recursive operation is performed.

The last step is the same as the first step. The change location is different. At this point, we hope to move the n-1 pillar on the second pillar to the third pillar, that is, Hanoi (n-1, B, A, C ). In this way, all recursive operations can be completed.



Code Implementation

The above algorithms seem complicated, but reading the code is actually very simple, as shown below:

<span style="font-family:Microsoft YaHei;">void HelloWorld::hanoi(unsigned int n, unsigned int a, unsigned int b, unsigned int c){if(n == 1){MOVE_NODE m ;m.s = a ;m.e = c ;m_MoveMement.push(m);m_out<< "Move from " << (char)a << " to " << (char)c << endl;} // end for n == 1else{//move the n-1 from a to bhanoi(n-1, a, c, b);//move the a to cMOVE_NODE m;m.s = a ;m.e = c ;m_MoveMement.push(m);m_out<< "Move from " << (char)a << " to " << (char)c << endl;//move the n-1 from b to chanoi(n-1, b, a, c);}}// end for hanoi</span>

When I first understood this algorithm, I couldn't understand it. I always want to move n columns on my own. However, this process is hard to come up with, so I always don't understand it. Later, I simply gave up moving and thought about the algorithm layer. I wanted to find out which one to which I first moved. Then we can solve the problem through this recursive idea. And print out the mobile step. In this game

Moving the experiment according to the output steps is indeed feasible. That is to say, this algorithm describes the root of the recursive call tree. If you want to know how to move the first step, you need to find the first subnode of the recursive call tree, but for our brain, it is difficult to go deep into so many layers that it is hard to imagine when the number of disks increases. We can analyze the algorithm to check the time complexity of the algorithm.

The basic operation of this algorithm should be the operation of moving. The number of calls of this operation is also recursive. The recursive relationship of the number of calls of this operation is as follows:

X (n) = x (n-1) + 1 + x (n-1) where the first x (n-1) indicates the number of times the basic operation is called in the first step in the summary, while 1 indicates that only one basic operation is called in the current recursion, and the remaining X (n-1) the number of times the basic operation is called in the third step in the summary. Obviously, this frequency relationship is actually a recursive relationship. The following formula can be obtained through recursive derivation:

X (n) = 2 ^ n-1

That is to say, the complexity of this algorithm is O (2 ^ N), an exponential algorithm. Although this algorithm is elegant, its efficiency is not flattering. When the number of disks reaches 64, think about how much time it will take to move.

In order to conceal the algorithm, I use cocos2d-x to develop a dynamic algorithm demo program, interested readers can download on their own:

Hanoi.zip

In this program, there is a file named config. ini, which has two parameters, the number of disks and the time to move a disc. You can adjust these two parameters by yourself. At the same time, the program will save the sudden output in a file named step.txt. Readers can follow this step to complete the above game !!!

Hanoi of 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.