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"); } }
相关文章推荐
- poj 1208 The Blocks Problem (链表模拟)
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- POJ-模拟-1208 The Blocks Problem
- POJ 1208 The Blocks Problem 栈模拟 练STL
- poj 1208 The Blocks Problem 模拟+vector的使用
- POJ 1208 The Blocks Problem [模拟]
- POJ 1208 The Blocks Problem (模拟)
- POJ 1208 TheBlocks Problem (模拟+队列)
- POJ 1208 The Blocks Problem (UVA 101)
- POJ1208 -- The Blocks Problem
- POJ 1208 The Blocks Problem
- POJ 1208 The Blocks Problem
- POJ 1208 The Blocks Problem
- poj 1208 The Blocks Problem
- POJ 1208 The Blocks Problem
- POJ 1208 The Blocks Problem
- POJ 1208 The Blocks Problem 链表
- POJ 1208 The Blocks Problem 简单栈模拟
- POJ-1208-The Blocks Problem