Test instructions: Give the n block from left to right (numbering from 0), then give four kinds of operations, and then give the corresponding operation, the output after the end of each pile of wood block situation.
Study Purple Book, because each pile of wood number is in the change, so with the vector. Then there are simulations of several operations
Learn this & in this function, Find_block (Inta,int &p,int&h)
The note written on the Purple book is "Finding the pile and height of a block a, returning the caller in the form of a reference"
At first did not understand = = then wrote a small program to test the
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 voidFint&h)8 {9H=1;TenH=2; OneH=555; A } - - intMain () the { - intha; - f (HA); -printf"ha=%d\n", ha); +}
Should be the value of the last p to how much, return this value to the value of the corresponding argument
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 using namespacestd;7 8 Const intmaxn= -;9 intN;Tenvector<int>PILE[MAXN]; One A voidFind_block (intAint&p,int&H)//find out which piles of wood A are in, and the height of the pile they are in. - { - for(p=0;p <n;p++) the for(h=0; H<pile[p].size (); h++) - if(pile[p][h]==a)return; - } - + voidClear_above (intPintH//put the p team above the height of H to the original position - { + for(inti=h+1; I<pile[p].size (); i++) A { at intb=Pile[p][i]; -Pile[b].push_back (b);//The original position of Block B is in heap B - } -Pile[p].resize (H +1); - } - in voidPile_onto (intPintHintP2)//Move the block above the height of the P heap to h and above to the P2 heap - { to for(intI=h;i<pile[p].size (); i++) + Pile[p2].push_back (Pile[p][i]); - pile[p].resize (h); the } * $ voidPrint ()//output of each heapPanax Notoginseng { - for(intI=0; i<n;i++) the { +printf"%d:", i); A for(intj=0; J<pile[i].size (); j + +) theprintf"%d", Pile[i][j]); +printf"\ n"); - } $ } $ - intMain () - { the intb; -Cin>>N;Wuyi strings1,s2; the for(intI=0; i<n;i++) - Pile[i].push_back (i); Wu while(cin>>s1>>a>>s2>>b) - { About intHA,HB,PA,PB; $ Find_block (a,pa,ha); - Find_block (B,PB,HB); - if(PA==PB)Continue; - if(s2=="onto") Clear_above (PB,HB);//when the statement contains onto, it is necessary to put the top of the B heap back to the original position A if(s1=="Move") Clear_above (PA,HA);//when a move is contained in a statement, it is necessary to place the top of the a heap back in its original position. + Pile_onto (PA,HA,PB); the } - print (); $ return 0; the}
View Code
UVa 101 The Blocks problem