POJ 1208 The Blocks Problem(模拟)

这题就是一道简单模拟题,总共有4个操作,然后给你一个操作序列,最后输出最后的木块排列方式。这题直接用vector随便搞了,反正规模也很小,用vector的相关操作也不会超时,然后就简单模拟一下,之前有个地方看了一眼没注意,写了半天发现死循环,后来看了一下题解上的题意发现在一堆里面的两块操作是非法的,不做变动,改了一下就没什么问题了,不过交上去PE了一次,原来是少输出个空格,无语了……
#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <cstdlib>
using namespace std;
char str1[10],str2[10];
vector<int> num[30];
vector<int>::iterator it;
int pos[30];
int main()
{
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        num[i].clear();
        num[i].push_back(i);
        pos[i]=i;
    }
    int x,y;
    while(scanf("%s",str1))
    {
        if(str1[0]=='q')
            break;
        scanf("%d%s%d",&x,str2,&y);
        if(pos[x]==pos[y])
            continue;
        if(str1[0]=='m')
        {
            if(str2[1]=='n')
            {
                bool ok=false;
                for(it=num[pos[y]].begin(); it!=num[pos[y]].end();)
                {
                    if(*it==y)
                    {
                        ok=true;
                        it++;
                        continue;
                    }
                    if(ok)
                    {
                        int temp=*it;
                        num[temp].insert(num[temp].begin(),temp);
                        it=num[pos[y]].erase(it);
                        pos[temp]=temp;
                    }
                    else it++;
                }
                ok=false;
                for(it=num[pos[x]].begin(); it!=num[pos[x]].end();)
                {
                    if(*it==x)
                    {
                        ok=true;
                        num[pos[y]].push_back(x);
                        it=num[pos[x]].erase(it);
                        continue;
                    }
                    if(ok)
                    {
                        int temp=*it;
                        num[temp].insert(num[temp].begin(),temp);
                        it=num[pos[x]].erase(it);
                        pos[temp]=temp;
                    }
                    else it++;
                }
            }
            else
            {
                bool ok=false;
                for(it=num[pos[x]].begin(); it!=num[pos[x]].end();)
                {
                    if(*it==x)
                    {
                        num[pos[y]].push_back(x);
                        ok=true;
                        it=num[pos[x]].erase(it);
                        continue;
                    }
                    if(ok)
                    {
                        int temp=*it;
                        num[temp].insert(num[temp].begin(),temp);
                        it=num[pos[x]].erase(it);
                        pos[temp]=temp;
                    }
                    else it++;
                }
            }
            pos[x]=pos[y];
        }
        else
        {
            if(str1[1]=='n')
            {
                bool ok=false;
                for(it=num[pos[y]].begin(); it!=num[pos[y]].end();)
                {
                    if(*it==y)
                    {
                        ok=true;
                        it++;
                        continue;
                    }
                    if(ok)
                    {
                        int temp=*it;
                        num[temp].insert(num[temp].begin(),temp);
                        it=num[pos[y]].erase(it);
                        pos[temp]=temp;
                    }
                    else it++;
                }
                ok=false;
                for(it=num[pos[x]].begin(); it!=num[pos[x]].end();)
                {
                    if(*it==x)
                        ok=true;
                    if(!ok)
                    {
                        it++;
                        continue;
                    }
                    num[pos[y]].push_back(*it);
                    pos[*it]=pos[y];
                    it=num[pos[x]].erase(it);
                }
            }
            else
            {
                bool ok=false;
                for(it=num[pos[x]].begin(); it!=num[pos[x]].end();)
                {
                    if(*it==x)
                        ok=true;
                    if(!ok)
                    {
                        it++;
                        continue;
                    }
                    int temp=*it;
                    num[pos[y]].push_back(temp);
                    if(temp!=x)
                        pos[temp]=pos[y];
                    it=num[pos[x]].erase(it);
                }
            }
            pos[x]=pos[y];
        }

    }
    for(int i=0; i<n; i++)
    {
        printf("%d:",i);
        for(it=num[i].begin(); it!=num[i].end(); it++)
                printf(" %d",*it);
        printf("\n");
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值