UVa 101 - The Blocks Problem

meaning of the title

Four Operations of Simulated Wood Blocks

move a onto b: put all the blocks above a and B in place, and then lay a on B.
move a over b: place all the blocks above a, and then put a on the top of the pile where B is.
pile a onto b: put all the blocks above B in place, and then stack a and the blocks above B on top of B as a whole.
pile a over b: stack a and the above blocks on top of the pile where B is.

thinking

Simple vector Simulation
When simulating, first find the location of the serial number, and then operate it.

Record

vec.resize() Respecifies the length of vec

AC code

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int maxn = 50;
vector<int> p[maxn];
int n;

void print();

void guiwei( int x, int h ){
    for( int i = h + 1; i < p[x].size(); i++ )
        p[ p[x][i] ].push_back(p[x][i]);
    p[x].resize(h+1);
}

void found( int a, int &h, int &x )
{
    for( x = 0; x < n; x++)
        for( h = 0; h < p[x].size(); h++)
            if(p[x][h] == a)
                return;
}

void pile_onto(int xa, int h, int xb)
{
    for(int i = h; i < p[xa].size(); i++)
        p[xb].push_back(p[xa][i]);
    p[xa].resize(h);
}

int main()
{
    string s1, s2;
    int a, b;
    cin >> n;
    for( int i = 0; i < n; i++ )
        p[i].push_back(i);
    while( cin >> s1 && s1 != "quit" ){
        cin >> a >> s2 >> b;
        int ha, hb, xa, xb;
        found( a, ha, xa );
        found( b, hb, xb );
        if( xa == xb )  continue;
        if(s2 == "onto") guiwei(xb, hb);
        if(s1 == "move") guiwei(xa, ha);
        pile_onto( xa, ha, xb );
    }
    print();
    return 0;
}

void print()
{
    for(int i = 0; i < n; i++){
        printf("%d:", i);
        for(int j = 0; j < p[i].size(); j++)
            printf(" %d", p[i][j]);
        puts("");
    }
}

Posted by oriental_express on Thu, 07 Feb 2019 09:00:17 -0800