uva 101

题意:
  • move a onto b
    • where a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions.
    • a和b都是积木的编号,先将a和b上面所有的积木都放回原处,再将a放在b上。
  • move a over b
    • where a and b are block numbers, puts block a onto the top of the stack containing block b, after returning any blocks that are stacked on top of block a to their initial positions.
    • a和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上。(b上原有积木不动)
  • pile a onto b
    • where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are moved to their initial positions prior to the pile taking place. The blocks stacked above block a retain their order when moved.
    • a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上。在移动前要先将b上面所有的积极都放回原处。移动的一摞积木要保持原来的顺序不变。
  • pile a over b
    • where a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto the top of the stack containing block b. The blocks stacked above block a retain their original order when moved.
    • a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b所在一摞积木的最上面一个积木上。移动的一摞积木要保持原来的顺序不变。
  • quit
    • terminates manipulations in the block world.
    • 结束积木世界的操纵。

Any command in which a = b or in which a and b are in the same stack of blocks is an illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks.
当a = b或a和b处在同一摞时,任何企图操作a和b的命令都是非法的。所有非法的命令都要忽略,且不能对当前积木的状态产生作用。

之后就是单纯的模拟了,细心一点就不会错的

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

struct blocks
{
    int m,n;
    char a[10];
    char b[10];
}AI[1005];
int cn[255][255],ct[255];

int sfind(int num,int m)      //找出m所在的堆
{
    for(int i=0; i<num; i++)
        for(int j=0; j<ct[i]; j++)
            if(cn[i][j]==m)
                return i;
    return 0;
}

int change(int flagm,int m)  //将m上的积木放回原处
{
    for(int i=ct[flagm]-1; i>=0; i--)
        if(cn[flagm][i]!=m)
        {
            int ggt=cn[flagm][i];
            cn[ggt][ct[ggt]++]=cn[flagm][i];
            ct[flagm]--;
        }
        else break;
    return 0;
}

int monto(int m,int n,int num) 
{
    int flagm,flagn;
    flagm=sfind(num,m);
    flagn=sfind(num,n);
    change(flagm,m);
    change(flagn,n);
    cn[flagn][ct[flagn]++]=cn[flagm][--ct[flagm]];
    return 0;
}

int mover(int m,int n,int num)
{
    int flagm,flagn;
    flagm=sfind(num,m);
    flagn=sfind(num,n);
    change(flagm,m);
    cn[flagn][ct[flagn]++]=cn[flagm][--ct[flagm]];
    return 0;
}

int ponto(int m,int n,int num)
{
    int flagm,flagn,flaga;
    flagn=sfind(num,n);
    flagm=sfind(num,m);
    change(flagn,n);
    for(int i=0; i<ct[flagm]; i++)
        if(m==cn[flagm][i])
        {
            flaga=i;
            break;
        }
    for(int i=flaga; i<ct[flagm]; i++)
        cn[flagn][ct[flagn]++]=cn[flagm][i];
    ct[flagm]-=ct[flagm]-flaga;
    return 0;
}

int pover(int m,int n,int num)
{
    int flagm,flagn,flaga;
    flagn=sfind(num,n);
    flagm=sfind(num,m);
    for(int i=0; i<ct[flagm]; i++)
        if(m==cn[flagm][i])
        {
            flaga=i;
            break;
        }
    for(int i=flaga; i<ct[flagm]; i++)
        cn[flagn][ct[flagn]++]=cn[flagm][i];
    ct[flagm]-=ct[flagm]-flaga;
    return 0;
}

int main()
{
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        int count=0;
        while(1)
        {
            getchar();
            scanf("%s",AI[count].a);
            if(strcmp(AI[count].a,"quit")==0)
                break;
            scanf(" %d %s %d",&AI[count].m,AI[count].b,&AI[count].n);
            count++;
        }
        for(int i=0; i<num; i++)
        {
            cn[i][0]=i;
            ct[i]=1;
        }
        for(int i=0; i<count; i++)
        {
            int flagm,flagn;
            flagn=sfind(num,AI[i].n);
            flagm=sfind(num,AI[i].m);
            if(flagm==flagn||AI[i].n==AI[i].m)    // notice
                continue;
            if(strcmp(AI[i].a,"move")==0&&strcmp(AI[i].b,"onto")==0)
                monto(AI[i].m,AI[i].n,num);
            else if(strcmp(AI[i].a,"move")==0&&strcmp(AI[i].b,"over")==0)
                mover(AI[i].m,AI[i].n,num);
            else if(strcmp(AI[i].a,"pile")==0&&strcmp(AI[i].b,"onto")==0)
                ponto(AI[i].m,AI[i].n,num);
            else if(strcmp(AI[i].a,"pile")==0&&strcmp(AI[i].b,"over")==0)
                pover(AI[i].m,AI[i].n,num);
        }
 		for(int i=0; i<num; i++)
        {
            printf("%d:",i);
            for(int j=0; j<ct[i]; j++)
                printf(" %d",cn[i][j]);
            puts("");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值