POJ-1208(The Blocks Problems)

题目链接: POJ - 1028

notice :
Any command in which a = b or in which a and b are in the same stack of
blocks is an illegal command. All illegal commands should be ignored and
should have no affect on the configuration of blocks.  
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

const int MAXN = 27;

const char MOVE[] = "move";
const char PILE[] = "pile";
const char OVER[] = "over";
const char ONTO[] = "onto";
const char QUIT[] = "quit";
vector<int> Stack[MAXN];
char cmd1[100], cmd2[100];
struct _Position{
    int stack;
    int pos;
    _Position(int s, int p):pos(p), stack(s){
    }
};
void init()
{
    for(int i = 0; i < MAXN; ++i)Stack[i].clear(), Stack[i].push_back(i);
}

void Print(int n)
{
    for(int i = 0; i < n; ++i)
    {
        int len = Stack[i].size();
        printf("%d:", i);

        if(len != 0)
        {
            for(int j = 0; j < len; ++j)
            {
                printf(" %d", Stack[i][j]);
            }
        }
        printf("\n");
    }
}

_Position findBlock(int n, int x)
{
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < Stack[i].size(); ++j)
            if(x == Stack[i][j]) return _Position(i, j);
}

void move(_Position pa, _Position pb, char cmd2[])
{
    //cout << pa.stack << "--" << pa.pos <<endl;
    vector<int>::iterator itas = Stack[pa.stack].begin(), itbs = Stack[pb.stack].begin();
    vector<int>::iterator itae = Stack[pa.stack].end(), itbe = Stack[pb.stack].end();
    if(strcmp(cmd2, ONTO) == 0)
    {
        Stack[pb.stack].insert(itbs + pb.pos + 1, Stack[pa.stack][pa.pos]);
    }else{
        Stack[pb.stack].push_back(Stack[pa.stack][pa.pos]);
    }
    Stack[pa.stack].erase(itas + pa.pos, itas + pa.pos + 1);
}

void pile(_Position pa, _Position pb, char cmd2[])
{
    //cout << pa.stack << "--" << pa.pos <<endl;
    vector<int>::iterator itas = Stack[pa.stack].begin(), itbs = Stack[pb.stack].begin();
    vector<int>::iterator itae = Stack[pa.stack].end(), itbe = Stack[pb.stack].end();
    if(strcmp(cmd2, ONTO) == 0)
    {
        Stack[pb.stack].insert(itbs + pb.pos, itas + pa.pos, itae);
    }else{
        Stack[pb.stack].insert(itbe, itas + pa.pos, itae);
    }
    Stack[pa.stack].erase(itas + pa.pos, itae);
}


int main()
{
    int n, a, b;
    while(cin >> n)
    {
        init();
        while(cin >> cmd1)
        {
            if(strcmp(cmd1, QUIT) == 0) break;
            cin >> a >> cmd2 >> b;
            _Position pa = findBlock(n, a);
            _Position pb = findBlock(n, b);
            if(pa.stack == pb.stack) continue;

            if(strcmp(cmd1, MOVE) == 0) move(pa, pb, cmd2);
            else pile(pa, pb, cmd2);
        }
        Print(n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值