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:
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn]; // 每個pile[i]是一個vector
// 找木塊a所在的pile和height,以引用的形式返回
void find_block(int a, int& p, int& h) {
for (p = 0; p < n; p++)
for (h = 0; h < pile[p].size(); h++)
if (pile[p][h] == a) return;
}
// 把第p堆高度為h的木塊上方的所有木塊移回原位
void clear_above(int p, int h) {
for (int i = h + 1; i < pile[p].size(); i++) {
int b = pile[p][i];
pile[b].push_back(b); // 把木塊b放回原位
}
pile[p].resize(h + 1); // pile只需保留0~h的元素
}
// 把第p堆高度h及其上方的木塊全部移動到p2堆的最上方
void pile_onto(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() {
int a, b;
cin >> n;
string s1, s2;
for (int i = 0; i < n; i++) pile[i].push_back(i);
while (cin >> s1 >> a >> s2 >> b) {
int pa, pb, ha, hb;
find_block(a, pa, ha);//找出位置
find_block(b, pb, hb);
if (pa == pb) continue; //非法指令
if (s2 == "onto") clear_above(pb, hb);
if (s1 == "move") clear_above(pa, ha);
pile_onto(pa, ha, pb);
}
print();
return 0;
}