您的位置:首页 > 其它

POJ 1208 The Blocks Problem(模拟)

2012-09-25 13:51 459 查看
这题就是一道简单模拟题,总共有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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vector iterator