#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
#define MAX 28
#define MOVE 0
#define ONTO 1
#define QUIT 2
vector<int> blocks[MAX];
const string cmd[] = { "move","onto","quit" };
int n;
void ReSet(int indexOfBlock, int height)
{
int size = blocks[indexOfBlock].size();
for (int i = height + 1;i != size;++i)
{
blocks[i].push_back(blocks[indexOfBlock][i]);//还原height以上的木块
}
blocks[indexOfBlock].resize(height + 1);//清除被还原的木块
}
void FindSite(int block, int &indexOfBlock, int &height)
{
for (int i = 0;i != n;++i)
{
int size = blocks[i].size();
for (int j = 0;j != size;++j)
{
if (blocks[i][j] == block)
{
indexOfBlock = i;
height = j;
return;
}
}
}
}
void pile(int a_index, int a_height, int b_index)//将a及其上的木块堆在b的顶部
{
int size = blocks[a_index].size();
for (int i = a_height; i != size; ++i)
{
blocks[b_index].push_back(blocks[a_index][i]);
}
blocks[a_index].resize(a_height);
}
void print()
{
for (int i = 0;i != n;++i)
{
printf("%d:", i);
int size = blocks[i].size();
for (int j = 0;j != size;++j)
{
printf(" %d", blocks[i][j]);
}
printf("\n");
}
}
int main()
{
cin >> n;
for (int i = 0;i != n;++i)
{
blocks[i].push_back(i);
}
int a, a_index, a_height, b, b_index, b_height;
string cmd_oper, cmd_type;
while (cin >> cmd_oper >> a >> cmd_type >> b)
{
if (cmd_oper == cmd[QUIT])
{
break;
}
FindSite(a, a_index, a_height);
FindSite(b, b_index, b_height);
if (a_index == b_index)
{
continue;
}
if (cmd_type == cmd[ONTO])
{
ReSet(b_index, b_height);
}
if (cmd_oper == cmd[MOVE])
{
ReSet(a_index, a_height);
}
pile(a_index, a_height, b_index);
}
print();
return 0;
}
主要在于提取几个命令的共同点来简化问题