101 - The Blocks Problem
来源:互联网 发布:罗马里奥实况数据 编辑:程序博客网 时间:2024/05/16 12:50
#include <iostream>#include <cstdio>#include <string>#include <list>#include <vector>#include <cstring>#include <algorithm>using namespace std;vector <list<int> > l;int move(int a,int b,int c){ list<int>::iterator p,p1; int t(0),k(0); for(int j=0; j<l.size(); j++) { p=find(l[j].begin(),l[j].end(),a); if(p!=l[j].end()) { t=j; break; } } for(int j=0; j<l.size(); j++) { p=find(l[j].begin(),l[j].end(),b); if(p!=l[j].end()) { k=j; break; } } if(k==t)return 0; if(c==1) { p=l[t].end(); p--; for(; p!=l[t].begin(); p--) if(*p!=a) { l[*p].push_back(*p); l[t].pop_back(); } else break; p=l[k].end(); p--; for(; p!=l[k].begin(); p--) if(*p!=b) { l[*p].push_back(*p); l[k].pop_back(); } else break; l[k].push_back(a); l[t].pop_back(); } else if(c==2) { p=l[t].end(); p--; for(; p!=l[t].begin(); p--) if(*p!=a) { l[*p].push_back(*p); l[t].pop_back(); } else break; l[k].push_back(a); l[t].pop_back(); } else if(c==3) { p=l[k].end(); p--; for(; p!=l[k].begin(); p--) if(*p!=b) { l[*p].push_back(*p); l[k].pop_back(); } else break; int sum(0); for(p=l[t].begin(); p!=l[t].end(); p++) { if(*p==a) { sum=1; p1=p; } if(sum==1) l[k].push_back(*p); } l[t].erase(p1,l[t].end()); } else if(c==4) { int sum(0); for(p=l[t].begin(); p!=l[t].end(); p++) { if(*p==a) { sum=1; p1=p; } if(sum==1) l[k].push_back(*p); } l[t].erase(p1,l[t].end()); } return 0;}int main(){ l.clear(); //freopen("a.txt","r",stdin); int n,x,y,flag; cin>>n; for(int i=0; i<n; i++) { list<int>v; v.push_back(i); l.push_back(v); } char str1[5], str2[5]; memset(str1,0,sizeof(str1)); while(cin>>str1) { if(strcmp(str1,"quit")==0) { list<int>::iterator p; for(int i=0; i<n; i++) { cout<<i<<":"; for(p=l[i].begin(); p!=l[i].end(); p++) cout<<" "<<*p; cout<<endl; } return 0; } memset(str2,0,sizeof(str2)); cin>>x>>str2>>y; flag=0; if(strcmp(str1,"move")==0&&strcmp(str2,"onto")==0) { flag=1; move(x,y,flag); } else if(strcmp(str1,"move")==0&&strcmp(str2,"over")==0) { flag=2; move(x,y,flag); } else if(strcmp(str1,"pile")==0&&strcmp(str2,"onto")==0) { flag=3; move(x,y,flag); } else if(strcmp(str1,"pile")==0&&strcmp(str2,"over")==0) { flag=4; move(x,y,flag); } memset(str1,0,sizeof(str1)); } return 0;}