uva 101

数据结构+模拟~

题意大概有N个木块,有4种操作。

move a onto b 将a木块和b木块上面的木块移回最开始的地方(这个最坑了,英语真心一开始木有看懂。)然后把a移到b上面。

move a over b 只把a木块上面的木块移到原来的位置,然后把a移到有b的一堆木块上即可(不论b是否是最上面)

pile a onto b 把a和a上面的一堆木块移动到b上面,b上面的木块移回到最初的位置。

pile a over b 把a上面的一堆移动到b上面的一堆。

AC代码:

#include<cstdio>
#include<cstring>
#include<stack>
using  namespace std;
const int MAXN=30;
int place[MAXN];                                //存每个木块的位置
stack<int>sta[MAXN];                            //每一堆用栈表示
void move_onto(int a,int b){                    //模拟move onto的操作
    while(sta[place[b]].top()!=b){              //将b上面的木块移回原来的位置,记得改变place,和清除已移动的木块
        int temp=sta[place[b]].top();
        sta[place[b]].pop();
        sta[temp].push(temp);
        place[temp]=temp;
    }
    while(sta[place[a]].top()!=a){
        int temp=sta[place[a]].top();
        sta[place[a]].pop();
        sta[temp].push(temp);
        place[temp]=temp;
    }
    sta[place[a]].pop();
    place[a]=place[b];
    sta[place[b]].push(a);
}
void move_over(int a,int b){               //只用移动a的,然后放在有b的堆上面
    while(sta[place[a]].top()!=a){
        int temp=sta[place[a]].top();
        sta[place[a]].pop();
        sta[temp].push(temp);
        place[temp]=temp;
    }
    sta[place[a]].pop();
    place[a]=place[b];
    sta[place[b]].push(a);
}
void pile_onto(int a,int b){
    while(sta[place[b]].top()!=b){
        int temp=sta[place[b]].top();
        sta[place[b]].pop();
        sta[temp].push(temp);
        place[temp]=temp;
    }
    int temp[MAXN],m=0;
    while(1){                                    //临时存一个数组将要移动的一堆记录,stack只能从顶端入,顶端出,要有中间过一次
        temp[m++]=sta[place[a]].top();
        sta[place[a]].pop();
        place[temp[m-1]]=place[b];
        if(temp[m-1]==a) break;
    }
    for(int i=m-1;i>=0;i--) sta[place[b]].push(temp[i]);
}
void pile_over(int a,int b){
    int temp[MAXN],m=0;
    while(1){
        temp[m++]=sta[place[a]].top();
        sta[place[a]].pop();
        place[temp[m-1]]=place[b];
        if(temp[m-1]==a) break;
    }
    for(int i=m-1;i>=0;i--) sta[place[b]].push(temp[i]);
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    memset(place,0,sizeof(place));
    int n;
    scanf("%d",&n);
    char a[6],b[6];
    int c,d;
    for(int i=0;i<n;i++){                   //初始化
        place[i]=i;
        sta[i].push(i);
    }
    while(~scanf("%s",a)){
        if(strcmp(a,"quit")==0) break;
        scanf("%d%s%d",&c,b,&d);
        if(place[c]==place[d]) continue;
       // printf("%s %d %s %d\n",a,c,b,d);
        if(strcmp(a,"move")==0 && strcmp(b,"onto")==0)      move_onto(c,d);
        else if(strcmp(a,"move")==0 && strcmp(b,"over")==0) move_over(c,d);
        else if(strcmp(a,"pile")==0 && strcmp(b,"onto")==0) pile_onto(c,d);
        else if(strcmp(a,"pile")==0 && strcmp(b,"over")==0) pile_over(c,d);
    }
    int temp[MAXN];
    for(int i=0;i<n;i++){                    //输出时也要先存到临时数组里输出
        int  m=0;
        printf("%d:",i);
        while(!sta[i].empty()) {temp[m++]=sta[i].top();sta[i].pop();}
        for(int j=m-1;j>=0;j--) printf(" %d",temp[j]);
        printf("\n");
    }
    return 0;
}


 


 

 

 

 


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值