一道比较繁琐的模拟题, 非常能练习STL的
一开始WA了好多次, 原因是看漏了题目描述最后一句话, 对于不合法的命令要自己进行忽略处理.....
以后看题得耐心, 看完才下手
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 30;
typedef long long LL;
/*
uva 101
*/
char cmd[MAXN],str1[MAXN],str2[MAXN];
vector<int> blks[MAXN];
int pos[MAXN],n,from,to;
int main(){
// freopen("input2.txt","r",stdin);
scanf("%d",&n);
for(int i=0; i<n; i++){
pos[i] = i;
blks[i].push_back(i);
}
while( scanf("%s",cmd) && strcmp(cmd,"quit")!=0 ){
strcpy(str1,cmd);
scanf("%d %s %d",&from,str2,&to);
if( from==to || pos[from]==pos[to] ) continue;
if( strcmp(str1,"move")==0 ){
int t = pos[from]; // from所在的木块堆编号
// cout << blks[t].back() << endl;
while( blks[t].back()!=from ){
int top = blks[t].back();
pos[top] = top;
blks[top].push_back(top);
blks[t].pop_back();
}
if( strcmp(str2,"onto")==0 ){
t = pos[to];
while( blks[t].back()!=to ){
int top = blks[t].back();
pos[top] = top;
blks[top].push_back(top);
blks[t].pop_back();
}
}
// if( strcmp(str2,"over")==0 ){
blks[pos[from]].pop_back();
pos[from] = pos[to];
blks[pos[to]].push_back(from);
// }
}else{
if( strcmp(str2,"onto")==0 ){
int t = pos[to];
while( blks[t].back()!=to ){
int top = blks[t].back();
pos[top] = top;
blks[top].push_back(top);
blks[t].pop_back();
}
}
int t = pos[from];
vector<int> tmp;
while( blks[t].back()!=from ){
tmp.push_back(blks[t].back());
blks[t].pop_back();
}
tmp.push_back(blks[t].back());
blks[t].pop_back();
t = pos[to];
while( !tmp.empty() ){
pos[tmp.back()] = t;
blks[t].push_back(tmp.back());
tmp.pop_back();
}
}
}
for(int i=0; i<n; i++){
printf("%d:",i);
for(int j=0; j<blks[i].size(); j++){
printf(" %d",blks[i][j]);
}
cout << endl;
}
return 0;
}