In the classic question of the Nottingham, there are 3 towers and N different sizes of plates that can be used to pile up a tower. The plate must be piled from top to bottom in order from small to large (for example, any plate must be stacked on a plate larger than its size). At the same time, you must meet the following restrictions:
(1) only one plate can be moved at a time.
(2) after each plate is moved from the top of the heap, it can only be placed in the next heap.
(3) Each plate can only be placed on a plate larger than its size.
Please write a program that will move the first heap of plates to the last heap. Specific ideas in this note behind the record! The following is my C + + version of the detailed process! Including test procedures
1 classTower {2 Public:3 //Create three towers (i from 0 to 2)4Tower (inti) {5 }6 //Add A disk into the This tower7 voidAddintd) {8 if(!disks.empty () && disks.top () <=d) {9printf"Error placing disk%d\n", d);TenStd::cout <<"does not conform to error Disk.top:"<< disks.top () <<Std::endl; One}Else { A //cout << "successfully joined: Disks.push:" << d << Endl; - Disks.push (d); - } the } - - //@param t a tower - //Move The top disk of this tower to the top of T. + voidMovetopto (Tower &t) { - //Write Your code here + A //std::cout << "distks.top to join:" << disks.top () << Std::endl; at T.add (Disks.top ()); -Disks.pop ();//forget to drop the top member!//core missing sentence! Description is not familiar with the stack builder! - } - - //@param n An integer - //@param destination a tower in //@param buffer a tower - //Move n Disks from the This tower to the destination by buffer tower to voidMovedisks (intN, Tower &destination, Tower &buffer) { + //Write Your code here - if(N >0) { theMovedisks (n1, buffer,destination); * movetopto (destination); $Buffer.movedisks (N-1, Destination, * This);Panax Notoginseng } - } the +stack<int>getdisks () { A returndisks; the } + - Private: $stack<int>disks; $ }; - /** - * Your Tower object would be instantiated and called as such: the * vector<tower> Towers; - * for (int i = 0; i < 3; i++) Towers.push_back (Tower (i));Wuyi * for (int i = n-1; I >= 0; i--) towers[0].add (i); the * Towers[0].movedisks (n, towers[2], towers[1]); - * Print towers[0], towers[1], towers[2] Wu */
C + + full version reference:
1#include <iostream>2#include <stack>3 4 using namespacestd;5 6 structproblem7 {8 intN;9 CharSRC, Mid, DST;Ten problem () {} OneProblem (intNCharSCharMChard): N (n), SRC (s), Mid (M), DST (d) {} A }; - - intMain () the { - intN; -CIN >>N; -Stack<problem>Stk; + problem curprb; -Stk.push (Problem (n,'A','B','C')); + while(!Stk.empty ()) { ACURPRB =stk.top (); at Stk.pop (); - if(CURPRB.N = =1) -cout << curprb.src <<" -"<< CURPRB.DST <<Endl; - Else { -Stk.push (Problem (curprb.n-1, Curprb.mid, CURPRB.SRC, CURPRB.DST)); -cout << curprb.src <<" -"<< CURPRB.DST <<Endl; inStk.push (Problem (curprb.n-1, Curprb.src, CURPRB.DST, Curprb.mid)); - } to } +}
lintcode:227. Simulating Hanoi tower problems with stacks