/*测试后提交时忘记去掉测试的输出了。。结果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;
}
UVA - 101 - The Blocks Problem
最新推荐文章于 2024-07-30 19:40:03 发布