Test instructions
There are n pieces of wood and n blocks of wood, and the initial state is the block I on the block I block on the first block. There are four operations, and then the final state is output.
Analysis:
Use a vector<int> to simulate a block heap and perform the appropriate operation.
1#include <iostream>2#include <cstdio>3#include <vector>4#include <string>5 using namespacestd;6 7 Const intMAXN = -;8 intN;9vector<int>PILE[MAXN];Ten One voidFind_block (intAint& P,int&h) A { - for(inti =0; I < n; ++i) - for(intj =0; J < Pile[i].size (); ++j) the if(Pile[i][j] = =a) -{p = i; h =J;} - } - + voidClear_block (intPinth) -{//Place the block above the upper part of the p stack height to H + for(inti = h +1; I < pile[p].size (); ++i) A { at intK =Pile[p][i]; - Pile[k].push_back (k); - } -Pile[p].resize (H +1); - } - in voidMove_onto (intPaintHintpb) -{//Stack wood blocks with a PA stack height of h and above onto a PB heap to for(inti = h; I < pile[pa].size (); ++i) + Pile[pb].push_back (Pile[pa][i]); - pile[pa].resize (h); the } * $ intMain ()Panax Notoginseng { - //freopen ("In.txt", "R", stdin); the intA, B; +scanf"%d", &n); A for(inti =0; I < n; ++i) pile[i].push_back (i); the strings1, S2; + while(Cin >> S1 >> a >> s2 >>b) - { $ intPA, Pb, ha, HB; $ Find_block (A, PA, ha); - Find_block (b, Pb, HB); - if(PA = pb)Continue; the if(S2 = ="onto") Clear_block (Pb, HB); - if(S1 = ="Move") Clear_block (PA, ha);Wuyi Move_onto (PA, ha, pb); the } - for(inti =0; I < n; ++i) Wu { -printf"%d:", i); About for(intj =0; J < Pile[i].size (); ++J) printf ("%d", Pile[i][j]); $printf"\ n"); - } - - A return 0; +}
code June
UVa 101 (analog) The Blocks problem