Sample Input
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
Sample Output
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
题目大意是通过指定的4种指令来改变木块的位置。用vector创个数组,分解4个指令并封装方法即可。
贴代码:
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
using namespace std;
#define Start clock_t start = clock();
#define End clock_t end = clock();
#define Print cout<<(double)(end-start)/CLOCKS_PER_SEC*1000<<"毫秒"<<endl;
int n;
const int maxn = 30;
vector<int> pile[maxn];
void find_block(int a,int& p,int& h){
for(p = 0;p < n;p++){
for(h = 0;h < pile[p].size();h++){
if(a == pile[p][h])return;
}
}
}
void clear_block(int a,int p,int h){
for(int i = h+1;i < pile[p].size();i++){
int k = pile[p][i];
pile[k].push_back(k);
}
pile[p].resize(h+1);
}
void pile_on(int a,int p,int h,int p2){
for(int i = h;i < pile[p].size();i++){
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);
}
void print(){
for(int i = 0;i < n;i++){
printf("%d:",i);
for(int j = 0;j < pile[i].size();j++){
printf(" %d",pile[i][j]);
}
printf("\n");
}
}
int main()
{
//Start;
cin>>n;
for(int i = 0;i < n;i++) pile[i].push_back(i);
string s1,s2;
int a,b;
while(cin>>s1){
if(s1 == "quit") break;
cin>>a>>s2>>b;
int pa,ha,pb,hb;
find_block(a,pa,ha);
find_block(b,pb,hb);
if(pa == pb) continue;
else if(s1 == "move" && s2 == "onto") {clear_block(a,pa,ha);clear_block(b,pb,hb);}
else if(s1 == "move" && s2 == "over") {clear_block(a,pa,ha);}
else if(s1 == "pile" && s2 == "onto") {clear_block(b,pb,hb);}
pile_on(a,pa,ha,pb);
}
print();
//End;
//Print;
return 0;
}