Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用栈来模拟的

Posted by StepByStepCnmLife at 2015-05-14 16:09:20 on Problem 1208
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator