数据结构uva101

题意:1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;

            2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;

            3.pile a onto b,把b上面的放回原来位置,然后把a所在的堆整体放到b上面;

            4.pile a over b,吧a所在堆整体放到b所在堆的上面。

自己写的代码太挫了,代码美观性有待提高

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int place[25];
int stack[25][25];
int top[25];
void init_place( int a )
{
    int block,id = place[a];
    while ( stack[id][top[id]] != a ) {
        block = stack[id][top[id] --];
        place[block] = block;
        stack[block][++ top[block]] = block;
    }
}
int  temp[25];
void pile_a_to_b( int a, int b )
{
    int topt = -1,id = place[a],ID = place[b];
    while ( stack[id][top[id]] != a )
        temp[++ topt] = stack[id][top[id] --];
    place[a] = ID;
    stack[ID][++ top[ID]] = a;
    top[id] --;
    while ( topt >= 0 ) {
        place[temp[topt]] = ID;
        stack[ID][++ top[ID]] = temp[topt --];
    }
}

int main()
{
    int  n,a,b;
    char oper[5],type[5];
    while ( ~scanf("%d",&n) ) {
        for ( int i = 0 ; i < n ; ++ i ) {
            stack[i][0] = i;
            place[i] = i;
            top[i] = 0;
        }
        while ( scanf("%s",oper) && oper[0] != 'q' ) {
            scanf("%d%s%d",&a,type,&b);
            if ( place[a] == place[b] )
                continue;
            if ( oper[0] == 'm' )
                init_place( a );
            if ( type[1] == 'n' )
                init_place( b );
            pile_a_to_b( a, b );
        }

        for ( int i = 0 ; i < n ; ++ i ) {
            printf("%d:",i);
            int now = 0;
            while ( now <= top[i] )
                printf(" %d",stack[i][now ++]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值