1 #include <iostream> 2 #include <sstream> 3 #include <string> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 #define GI ({int _t; scanf("%d", &_t); _t;}) 9 #define FOR(i, a, b) for (int i=a; i<b; i++) 10 #define REP(i, a) FOR(i, 0, a) 11 template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();} 12 int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} 13 #define DBG(x) cout << #x << "::" << x << endl; 14 #define DBGV(_v) { REP(_i, _v.size()) { cout << _v[_i] << "\t";} cout << endl;} 15 vector < vector < int > > blocks; 16 17 void printer() { 18 REP(i, blocks.size()) { 19 printf("%d:", i); 20 if (blocks[i].size() > 0) printf(" "); 21 REP(j, blocks[i].size()) { 22 printf("%d", blocks[i][j]); 23 if (j != blocks[i].size()-1) printf(" "); 24 } 25 printf("\n"); 26 } 27 //printf("\n"); 28 return; 29 } 30 31 int main() { 32 string command, mode; 33 int from, to; 34 int n = GI; 35 REP(i, n) { 36 vector <int> t ; 37 t.push_back(i); 38 blocks.push_back(t); 39 } 40 // printer(); 41 int start_i, start_j, end_i, end_j, length; 42 while (cin >> command >> from >> mode >> to) { 43 // cout << command << "\t" << from << "\t" << mode << "\t" << to << endl; 44 REP(i, blocks.size()) { 45 REP(j, blocks[i].size()) { 46 if (blocks[i][j] == from) { start_i = i; start_j = j; } 47 if (blocks[i][j] == to) { end_i = i; end_j = j; } 48 } 49 } 50 if (start_i == end_i) { 51 //printf("Skipping....\n\n"); 52 continue; 53 } 54 if (command == "move" && mode == "onto") { 55 //Remove all the blocks above the destination block 56 length = blocks[end_i].size(); 57 for (int i = end_j+1; i<length; i++) { int cur = blocks[end_i][i]; blocks[cur].push_back(cur); } 58 for (int i = end_j+1; i<length; i++) { blocks[end_i].pop_back(); } 59 //Remove all the blocks from the source block 60 length = blocks[start_i].size(); 61 for (int i = start_j+1; i<length; i++) { int cur = blocks[start_i][i]; blocks[cur].push_back(cur); } 62 for (int i = start_j+1; i<length; i++) { blocks[start_i].pop_back(); } 63 64 blocks[start_i].pop_back(); 65 blocks[end_i].push_back(from); 66 } 67 else if (command == "move" && mode == "over") { 68 //Remove all the blocks from the source block 69 length = blocks[start_i].size(); 70 for (int i = start_j+1; i<length; i++) { int cur = blocks[start_i][i]; blocks[cur].push_back(cur); } 71 for (int i = start_j+1; i<length; i++) { blocks[start_i].pop_back(); } 72 73 blocks[start_i].pop_back(); 74 blocks[end_i].push_back(from); 75 } 76 else if (command == "pile" && mode == "over") { 77 length = blocks[start_i].size(); 78 for (int i = start_j; i < length; i++) { blocks[end_i].push_back(blocks[start_i][i]); } 79 for (int i = start_j; i < length; i++) { blocks[start_i].pop_back(); } 80 } 81 else if (command == "pile" && mode == "onto") { 82 //Remove all the blocks above the destination block 83 length = blocks[end_i].size(); 84 for (int i = end_j+1; i<length; i++) { int cur = blocks[end_i][i]; blocks[cur].push_back(cur); } 85 for (int i = end_j+1; i<length; i++) { blocks[end_i].pop_back(); } 86 //Move all the blocks from the start to the destination block 87 length = blocks[start_i].size(); 88 for (int i = start_j; i < length; i++) { blocks[end_i].push_back(blocks[start_i][i]); } 89 for (int i = start_j; i < length; i++) { blocks[start_i].pop_back(); } 90 } 91 //printer(); 92 } 93 printer(); 94 return 0; 95 }