UVa 101 The Blocks Problem

坑爹啊,中间输出没去掉,检查了好久……

略恶心的一道题- -

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    int st[30][30],a,b,n,top[30],i,j;
    char s1[10],s2[10];
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            st[i][0]=i;
            top[i]=1;
        }
        while(scanf("%s",s1)&&strcmp(s1,"quit"))
        {
            scanf("%d%s%d",&a,s2,&b);
            if(strcmp(s1,"move")==0)
            {
                if(strcmp(s2,"onto")==0)
                {
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==a)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int x=i,y=j;
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==b)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    if(x==i)
                        continue;
                    int y1=j;
                    for(j=y+1;j<top[x];j++)
                    {
                        st[st[x][j]][0]=st[x][j];
                        top[st[x][j]]=1;
                    }
                    top[x]=y;
                    for(j=y1+1;j<top[i];j++)
                    {
                        st[st[i][j]][0]=st[i][j];
                        top[st[i][j]]=1;
                    }
                    st[i][y1+1]=a;
                    top[i]=y1+2;
                }
                else if(strcmp(s2,"over")==0)
                {
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==a)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int x=i,y=j;
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==b)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    if(x==i)
                        continue;
                    for(j=y+1;j<top[x];j++)
                    {
                        st[st[x][j]][0]=st[x][j];
                        top[st[x][j]]=1;
                    }
                    top[x]=y;
                    st[i][top[i]]=a;
                    top[i]++;
                }
            }
            else if(strcmp(s1,"pile")==0)
            {
                if(strcmp(s2,"onto")==0)
                {
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==b)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int x=i,y=j;                   
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==a)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int y1=j;
                    if(x==i)
                        continue;
                    for(j=y+1;j<top[x];j++)
                    {
                        st[st[x][j]][0]=st[x][j];
                        top[st[x][j]]=1;
                    }
                    top[x]=y+1;
                    for(j=y1;j<top[i];j++)
                        st[x][top[x]++]=st[i][j];
                    top[i]=y1;
                }
                else if(strcmp(s2,"over")==0)
                {
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==b)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int x=i;
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<top[i];j++)
                            if(st[i][j]==a)
                                break;
                        if(j!=top[i])
                            break;
                    }
                    int y=j;
                    if(x==i)
                        continue;
                    for(;j<top[i];j++)
                        st[x][top[x]++]=st[i][j];
                    top[i]=y;
                    
                }
            } 
        }
        for(i=0;i<n;i++)
        {
            printf("%d:",i);
            for(j=0;j<top[i];j++)
                printf(" %d",st[i][j]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值