| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
用栈来模拟的#include <stdio.h >
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct Stack{
int arr[MAXSIZE];
int top;
}Stack;
Stack t[MAXSIZE];
Stack mid;
int addr[MAXSIZE];//记录每一个积木的当前位置
void push(Stack *p,int x)
{
p->arr[p->top++]=x;
}
int pop(Stack *p)
{
return (*p).arr[--(*p).top];
}
int GetTop(Stack p)
{
return p.arr[p.top-1];
}
void first_op(char *s1,int dt1)
{
int kth;
int x;
kth=addr[dt1];//说明在第kth个栈内
if(strcmp(s1,"move")==0)
{
while(1)
{
x=pop(&t[kth]);
if(x==dt1)
{
push(&mid,x);
break;
}
else
{
push(&t[x],x);
addr[x]=x;//更新x的位置
}
}
}
else//"pile"
{
while(1)
{
x=pop(&t[kth]);
push(&mid,x);
if(x==dt1)break;
}
}
}
void second_op(char *s2,int dt2)
{
int kth;
int x;
kth=addr[dt2];
if(strcmp(s2,"onto")==0)
{
while( GetTop(t[kth])!=dt2 )
{
x=pop(&t[kth]);
push(&t[x],x);
addr[x]=x;
}
}
while(mid.top!=0)
{
x=pop(&mid);
push(&t[kth],x);
addr[x]=kth;
}
}
int main()
{
int n,i,j,k,cnt;
char s[50],r[50];
int dt1,dt2;
scanf("%d",&n);
/***********init************/
for(i=0;i<n;i++)
{
t[i].top=0;
push(&t[i],i);
addr[i]=i;
}
/**********操作*************/
while(scanf("%s",s)==1&&s[0]!='q')
{
scanf("%d %s %d",&dt1,r,&dt2);
mid.top=0;
first_op(s,dt1);
second_op(r,dt2);
}
for(i=0;i<n;i++)
{
printf("%d:",i);
for(j=0;j<(t[i].top);j++)
printf(" %d",t[i].arr[j]);
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator