UVA - 101 - The Blocks Problem

/*测试后提交时忘记去掉测试的输出了。。结果WA了一次
总体太说题目不是很难,关键是细心,模拟好每一步。
写成一个函数太复杂,分块实现,结构清晰明了*/
#include<stdio.h>
#include<string.h>
int stack[30][30];
int top[30],tag[30];
int Search(int n)
{
    int i;
    for(i=0;i<=top[tag[n]];i++)
    {
        if(stack[tag[n]][i]==n)return i;
    }
}
int ReturnBlock(int n,int pos)
{
    int i,count=0,t,m;
    m=top[tag[n]];
    for(i=pos+1;i<=m;i++)
    {
        t=stack[tag[n]][i];
        stack[t][++top[t]]=t;
        tag[t]=t;
        count++;        
    }
    top[tag[n]]-=count;
    return 0;
} 
int ConnectBlock(int a,int b)
{
    stack[tag[b]][++top[tag[b]]]=a;
    stack[tag[a]][--top[tag[a]]];
    tag[a]=tag[b];
    return 0;
}  
int PileBlock(int a,int posa,int b)
{
    int i,t,count,n,s;
    count=0;
    s=tag[a];
    n=top[s];
    top[s]=posa-1; 
    for(i=posa;i<=n;i++)
    {
        t=stack[s][i];
        stack[tag[b]][++top[tag[b]]]=t;
        tag[t]=tag[b];
    }   
    return 0;
}                    
int Manipulate(char coma[],int a,char comb[],int b,int n)
{
    int i,j,k,posa,posb;
    if(tag[a]==tag[b])return 0;
    if(!strcmp(coma,"move"))
    {
        if(!strcmp(comb,"onto"))
        {
            posa=Search(a);
            posb=Search(b);
            ReturnBlock(a,posa);
            ReturnBlock(b,posb);
            ConnectBlock(a,b);
            return 1;
        }
        if(!strcmp(comb,"over"))
        {
            posa=Search(a);
            ReturnBlock(a,posa);
            ConnectBlock(a,b);
            return 2;
        }
    }
    if(!strcmp(coma,"pile"))
    {
        if(!strcmp(comb,"onto"))
        {
            posa=Search(a);
            posb=Search(b);
            ReturnBlock(b,posb);
            PileBlock(a,posa,b);
            return 3;
        }
        if(!strcmp(comb,"over"))
        {
            posa=Search(a);
            PileBlock(a,posa,b);
            return 4;
        }
    }                                                                                           
}    

int main()
{
    int n,a,b,i,j;
    char coma[10],comb[10];
    for(i=0;i<30;i++)
    {
        top[i]=0;
        tag[i]=i;
        stack[i][0]=i;
        for(j=1;j<30;j++)
        {
            stack[i][j]=0;
        }
    }        
    scanf("%d",&n);
    while(scanf("%s",coma))
    {
        if(!strcmp(coma,"quit"))break;
        scanf("%d%s%d",&a,comb,&b);
        if(a==b)continue;
        Manipulate(coma,a,comb,b,n);
    }
    for(i=0;i<n;i++)
    {
        printf("%d:",i);
        for(j=0;j<=top[i];j++)
        {
            printf(" %d",stack[i][j]);
        }
        printf("\n");
    } 
    return 0;
}        
    


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值