UVa 101

题意

模拟木块的四种操作

move a onto b:把a和b上方的木块全部归位,然后把a摞在b上面。
move a over b:把a上方的木块全部归位,然后把a放在b所在木块堆的顶部。
pile a onto b:把b上方的木块全部归位,然后把a及上面的木块整体摞在b上面。
pile a over b:把a及上面的木块整体摞在b所在木块堆的顶部。

思路

简单的vector模拟
模拟的时候每次先找该序号所在的位置, 然后再操作

记录

vec.resize() 重新指定vec的长度

AC代码

#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("");
    }
}
原文地址:https://www.cnblogs.com/JinxiSui/p/9740612.html