3.4 In the classic question Hanoi tower, there are 3 pillars and n different sizes of perforated discs, plates can slide into any one of the pillars. From the beginning, all plates are placed from the bottom up to the top of the first pillar (i.e. each plate can only be put on a larger plate). The following restrictions apply when moving a disc:
Only one plate can be moved at a time;
The plate can only slide out from the top of the pillar and move to the next pillar;
The plate can only be stacked on a plate bigger than it.
Please use the stack and write the program to move all the plates from the first pillar to the last pillar.
The use of recursive methods is implemented as follows:
#include <iostream>#include<stack>using namespacestd;structop{intbegin; intend; CharA; CharB; CharC; OP (intStartintEndintAintBintc): Begin (Start), End (end), a (a), B (b), C (c) {}};voidHanoiintStartintEndCharACharBCharC) {Stack<op>St; OP tmp (START,END,A,B,C); St.push (TMP); while(!St.empty ()) {tmp=St.top (); St.pop (); if(tmp.begin!=tmp.end) {St.push (OP (tmp.begin,tmp.end-1, TMP. B,tmp. A,tmp. C)); St.push (OP (tmp.end,tmp.end,tmp). A,tmp. B,tmp. C)); St.push (OP (tmp.begin,tmp.end-1, TMP. A,tmp. C,tmp. B)); } Elsecout<<"Move"<<tmp.begin<<" from"<<tmp. a<<" to"<<tmp. c<<Endl; }}intMain () {Hanoi (1,6,'A','B','C');}
The recursive solution of Hanoi is finished, but this is not the subject of the request. The topic asks for a stack to solve the problem. Recursive solution is also used in the stack, in each recursive call itself, the intermediate state parameters are pressed into the stack. But these operations are implicit in the system, so you don't have to worry about how it's going to stack up. If we are going to use the stack ourselves to implement this process, we have to consider the details.
Next, we explicitly use the stack to implement the stack-out process of these state parameters in the recursive process. First, we need to define a data structure to hold the parameters during the operation.
struct op{ int begin, end; Char SRC, BRI, DST; OP () { } op (intint int int int. int PDST): Begin (Pbegin), End (Pend), SRC (psrc), BRI (Pbri), DST (PDST) { }};
5 of these parameters indicate that there is a stack of discs on the post SRC, numbered from begin to end, to move them from Src to DST, in the middle can be aided by the pillar Bri. The end is equivalent to the N in the recursive solution, the SRC,BRI,DST and the recursive solution. So why do you define the begin variable? To determine if there is only one plate left on the pillar. If begin equals end, only the "last" disc is left on the pillar and can be moved. Of course, it is possible to use a different Boolean variable to indicate whether there is only one disk left, and the effect is the same. When it comes to recursion, there are a couple of states that go through the initial state to the final state:
(1001~n-10) (01~n-1, N) (001
These processes we now need to self-stack processing. When the stack is not processed, when the stack is processed. Therefore, the stack must be pressed against the actual steps to be operated. In the beginning, we will end up with a task that we want to complete. It sounds strange, actually, is to push a set of parameters into the stack:
Stack<op> St; St.push (OP (1
This set of parameters indicates that the column SRC has a 1~n disk, to move it to DST, you can use the pillar bri. When the stack St is not empty, constantly out of the stack, when the begin and end are not equal, three push operations (corresponding to the above four states, the adjacent state corresponding to a push operation, so that the state changes), push and the actual operation of the reverse order (because the stack is processed, the order is correct when the stack), if , begin is equal to end, then the "last" disk with the current problem size is left, and the Hanoi code is printed as follows:
C + + Implementation code:
#include <iostream>#include<stack>using namespacestd;structop{intbegin; intend; CharA; CharB; CharC; OP (intStartintEndintAintBintc): Begin (Start), End (end), a (a), B (b), C (c) {}};voidHanoiintStartintEndCharACharBCharC) {Stack<op>St; OP tmp (START,END,A,B,C); St.push (TMP); while(!St.empty ()) {tmp=St.top (); St.pop (); if(tmp.begin!=tmp.end) {St.push (OP (tmp.begin,tmp.end-1, TMP. B,tmp. A,tmp. C)); St.push (OP (tmp.end,tmp.end,tmp). A,tmp. B,tmp. C)); St.push (OP (tmp.begin,tmp.end-1, TMP. A,tmp. C,tmp. B)); } Elsecout<<"Move"<<tmp.begin<<" from"<<tmp. a<<" to"<<tmp. c<<Endl; }}intMain () {Hanoi (1, 5,'A','B','C');}
Operation Result:
Reference: http://www.cricode.com/304.html
careercup-Stack and queue 3.4