uva 老是打不开,代码没有提交成功。但应该是对的。。。
一道模拟题,用vector做的。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 26;
int n;
vector<int> stat[maxn];
void moveback(int id){
int i,j,mark,len;
for(i=0;i<n;i++){
for(j=0;j<stat[i].size();j++){
if(stat[i][j] == id) {
mark = i;
len = j;
break;
}
}
}
for(i=stat[mark].size()-1;i>len;i--){
stat[stat[mark][i]].push_back(stat[mark][i]);
stat[mark].pop_back();
}
}
void putonto(int a ,int b){
int i,j,mark1,mark2;
for(i=0;i<n;i++){
for(j=0;j<stat[i].size();j++){
if(stat[i][j] == a) mark1 = i;
if(stat[i][j] == b) mark2 = i;
}
}
if(mark1 == mark2) return;
stat[mark2].push_back(a);
stat[mark1].pop_back();
}
void putover(int a,int b){
int i,j,mark1,mark2,len;
for(i=0;i<n;i++){
for(j=0;j<stat[i].size();j++){
if(stat[i][j] == a) {mark1 = i;len = j;}
if(stat[i][j] == b) mark2 = i;
}
}
if(mark1 == mark2) return;
int marksize = stat[mark1].size();
for(i=len;i< marksize ;i++){
stat[mark2].push_back(stat[mark1][i]);
}
for(i=len;i< marksize ;i++){
stat[mark1].pop_back();
}
}
void print(){
int i,j,mark1,mark2;
for(i=0;i<n;i++){
cout<<i<<":";
for(j=0;j<stat[i].size();j++){
cout<<stat[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
int i,j,a,b;
string cmd;
string cr;
for(i=0;i<maxn;i++){
stat[i].push_back(i);
}
scanf("%d",&n);
while(cin>>cmd){
if(cmd == "quit") break;
else{
cin>>a>>cr>>b;
if(cmd == "move"){
moveback(a);
if(cr == "onto"){
moveback(b);
putonto(a,b);
}
else if(cr == "over"){
putonto(a,b);
}
else continue;
}
else if(cmd == "pile"){
if(cr == "onto"){
moveback(b);
putover(a,b);
}
else if(cr == "over"){
putover(a,b);
}
else continue;
}
else continue;
}
//print();
}
print();
return 0;
}