Data structure learning (C ++) - recursion [2] (4)

zhaozj2021-02-16  54

#include

#include

Using namespace std;

Class Needle

{

PUBLIC:

Needle () {a.push_back (100);} // Every pillar has a base

Void push (int N) {a.push_back (n);}

INT top () {return a.back ();

INT POP () {Int n = a.back (); a.POP_BACK (); return n;}

INT MOVENUM (INT N) {INT I = 1; While (a [i]> n) i ; returnif () - i;}

INT size () {return a.size ();

INT Operator [] (int N) {Return A [N];

Private:

Vector a;

}

Void Hanoi (int N)

{

Needle Needle [3], NS; // 3 pillars, NS is the storage stack when converting the column, borrowing the stack structure of Needle

Int Source = 0, Target, Target_m = 2, Disk, M = N;

For (int i = n; i> 0; i -) Needle [0] .push (i); // put N plates on a column

While (n) // The problem is n, start moving

{

IF (! m) {source = ns.pop (); target_m = ns.pop ();

M = needle [source] .MOVENUM (ns.pop ());} // After the disorder is moved, return to the original current column.

IF (m% 2) Target = target_m; else target = 3 - source - target_m; // Law 1 implementation

IF (NEEDLE [SOURCE] .top ()

{

Disk = nesedle [source] .top (); m -;

COUT << Disk << "Move" << (char) (Source 0x41) << "to" << (char) (target 0x41) << endl; // Displays the moving process

Needle [Target] .push (Needle [Source] .pop ()); // Plan on the top of the target column

IF (disk == n) {source = 1 - source; target_m = 2; m = - N;} Regularization 3

}

ELSE // Realization of Law 2

{

ns.push (Needle [Source] [Needle [Source] .size () - M]);

ns.push (target_m); ns.push;

M = Needle [Target] .MOVENUM (Needle [Source] .top ());

Target_m = 3 - Source - Target; Source = Target;

}

}

}

This algorithm has a lot of complex algorithm (recursive algorithm can be found online, and you can find a lot slower, it seems to be extra, however, this is practical. I don't know how it is still moving 64 players, but I guess it will not be recursively returned to 1 plate, then move - wait for recursive, it is estimated that the beard is a fight (can be still in the world Two stories). We must immediately decide how to move in the next step, just like I wrote above, this is the normal thinking of people, and use recursive thinking, when you want to move, the cucumber is cool. Just as we doing things, although I don't do this in this life this life, I must have completed it for future generations, not how it should be done in it, if it doesn't reach the final result, That is also guaranteed to advance in the right direction, rather than staying in the same way. It is understood that the gap between computer programming is actually and normal, and the way we do if we do it directly to implement it directly, it is not the best because of the actual relevance in the computer. There may not be existent in the middle, such as the inverse push depth of the human brain is limited, and the computer is much more than the human brain, the accuracy of memory, the computer is much more brain. This also leads to a common programmer and a senior programmer's algorithm often has a font of the an. Because the latter knows how the computer likes to think.

转载请注明原文地址:https://www.9cbs.com/read-22979.html

New Post(0)