uva101题解

我们可以从题目中抽取两种相同的动作:

  1. x x x上面的木块放回原来编号的位置
  2. x x x与上面的一摞木块移到编号为 y y y的位置

而当第二个操作为onto的时候,就将 b b b执行操作1,而后面如果第一操作为pile的话,直接对 a a a执行二号操作,否则对 a a a执行一号操作后在做一点处理即可。

流程图如下:

Created with Raphaël 2.3.0 开始 输入n i=1 i<=n? 木块堆i中放入i i+=1 输入命令 是quit? 输入a,参数和b arg是onto? 操作1(b) 命令是move? 操作1(a) 将a压入b中 操作2(a,b) yes no no yes no yes no
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> stck[30];
int n;
int find(int x){
    for(int i=0;i<x;i++){
        for(auto jp:stck[i]){
            if(jp==x) return i;
        }
    }
}
void onto(int x,int posx){
    bool fl=false;
    int cnt=0;
    for(auto ip:stck[posx]){
        if(fl) stck[ip].push_back(ip),cnt++;
        if(ip==x) fl=true;
    }
    stck[posx].resize(stck[posx].size()-cnt);
}
void pile(int x,int posx,int posy){
    bool fl=false;
    int cnt=0;
    for(auto ip:stck[posx]){
        if(ip==x) fl=true;
        if(fl) stck[posy].push_back(ip),cnt++;
    }
    stck[posx].resize(stck[posx].size()-cnt);
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++) stck[i].push_back(i);
    while(true){
        string instu,arg;
        int a,b,posa=-1,posb=-1;
        cin>>instu;
        if(instu=="quit") break;
        else{
            cin>>a>>arg>>b;
            posa=find(a);
            posb=find(b);
            if(a==b||posa==posb) continue;
            if(arg=="onto") onto(b,posb);
            if(instu=="pile") pile(a,posa,posb);
            else{
                onto(a,posa);
                stck[posb].push_back(a);
                stck[posa].pop_back();
            }//这里
        }
    }
    for(int i=0;i<n;i++){
        cout<<i<<":";
        for(auto jt:stck[i]) cout<<" "<<jt;
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值