Non-recursive Hans Tower

Source: Internet
Author: User

I recently attended a UniversityAlgorithmAfter the tutorial, I deeply understood the limitations of Recursive Algorithms in some situations, so I thought of the recursive algorithms in the Hans Tower Project I made two years ago. Can it be converted into a circular algorithm?

 

To simplify the process, I completed this algorithm on the VC ++ console.

To makeProgramMore clearly, two types of PEG (column Class) and disk (plate class) are defined. Of course, the content is much simpler. The class diagram is as follows:

Peg indicates column type

Disks: vector <int> indicates all the plates on this column.

Name: int indicates the column name. I numbered column 0, 1, and column 2. So that the next operation

Disk indicates the tray type.

Now: int indicates the column where the plate is located.

Wantto: int indicates the column to be moved.

 

Cloopmove is an algorithm for moving dishes cyclically.

The only external interface is loopmove (int n): void. The parameter n indicates several plates.

The following describes the implementation process of this function in detail:

1. Declare Three Column objects first

Peg P0 (0); // start Column

Peg p1 (1); // target Column

Peg P2 (2); // Auxiliary Column

Then place the plate on P1.

Put the three variables in the array for ease of operation

Peg pegs [3] = {P0, P1, P2 };

 

2. define all the plates next, and now all the plates are on column 0.

Vector <disk> disks;

For (INT I = 0; I <n; ++ I)

{

Disks. push_back (disk (0 ));

}

 

3. by mathematical induction, it is easy to obtain that the number of steps for moving n plates is 2n-1. Therefore, the task can be completed by repeating so many times (if the number of times is not calculated, an endless loop can also be used, and judge whether the game is over in a loop ). In each loop, the findmove () method is called to update the wantto attribute of each plate. The specific method is:

1. The biggest dish, I .e., the n-1 target, must be P1.

2. cycle from N-2 plate to 0th Plate

In the cycle, determine whether a plate larger than each plate has been moved to another place.

If not, it should be moved to "third party ".

Disks [I]. Wantto = 3-Disks [I + 1]. Now-Disks [I + 1]. Wantto; //CodeClassic

For example, disk N-1 is now in column 0, to move to column 1, then the N-2 column should be in column 2

If it is in place, the plate should also be on this pillar.

Disks [I]. Wantto = disks [I + 1]. Wantto;

 

4. After findmove is called, access from the smallest plate to see which one needs to be moved.

 

The above is the structure of all programs. It can be seen that there is still a lot of room for optimization, so you don't have to call findmove every time to judge.

In addition, my findmove algorithm can support semi-automatic operations. That is to say, it is not necessary to start from the status of all disks on P0, but from any status (of course, a small disk must be on the dashboard ).

 

In the project, we compared it with the recursive Hans tower and found that this version of the loop is correct. Download Code: Non-recursive Hans Tower

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.