fork download
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. using namespace std;
  6. vector<int> ttmp;
  7. int main(void)
  8. {
  9. int a;scanf("%d",&a);
  10. int p[a];
  11.  
  12. vector<int> v[a];
  13. for(int i=0;i<a;i++){
  14. v[i].push_back(i);
  15. p[i]=i;
  16. }
  17. string st1,st2;
  18. int a1,a2,tmp=0;
  19. while(cin>>st1){
  20. if (st1=="quit") break;
  21. cin>>a1>>st2>>a2;
  22. if (a1==a2) continue;
  23. if (st1=="move"){
  24. if (st2=="onto"){
  25. tmp=a;
  26. while(tmp!=a1){
  27. tmp=v[p[a1]].back();
  28. v[p[a1]].pop_back();
  29. v[tmp].push_back(tmp);
  30. p[tmp]=tmp;
  31. }
  32. tmp=a;
  33. while(tmp!=a2){
  34. tmp=v[p[a2]].back();
  35. v[p[a2]].pop_back();
  36. v[tmp].push_back(tmp);
  37. p[tmp]=tmp;
  38. }
  39. tmp=v[p[a1]].back();
  40. v[p[a1]].pop_back();
  41. v[p[a2]].push_back(tmp);
  42. p[tmp]=p[a2];
  43. }else if (st2=="over"){
  44. tmp=a;
  45. while(tmp!=a1){
  46. tmp=v[p[a1]].back();
  47. v[p[a1]].pop_back();
  48. v[tmp].push_back(tmp);
  49. p[tmp]=tmp;
  50. }
  51. tmp=v[p[a1]].back();
  52. v[p[a1]].pop_back();
  53. v[p[a2]].push_back(tmp);
  54. p[tmp]=p[a2];
  55. }
  56. }else if (st1=="pile"){
  57. if (st2=="onto"){
  58. tmp=a;
  59. while(tmp!=a2){
  60. tmp=v[p[a2]].back();
  61. v[p[a2]].pop_back();
  62. v[tmp].push_back(tmp);
  63. p[tmp]=tmp;
  64. }
  65. tmp=a;
  66. while(tmp!=a1){
  67. tmp=v[p[a1]].back();
  68. v[p[a1]].pop_back();
  69. ttmp.push_back(tmp);
  70. }ttmp.push_back(a1);
  71. while(!ttmp.empty()){
  72. tmp=ttmp.back();
  73. ttmp.pop_back();
  74. v[p[a2]].push_back(tmp);
  75. p[tmp]=p[a2];
  76. }
  77. }else if (st2=="over"){
  78. tmp=a;
  79. while(tmp!=a1){
  80. tmp=v[p[a1]].back();
  81. v[p[a1]].pop_back();
  82. ttmp.push_back(tmp);
  83. }ttmp.push_back(a1);
  84. while(!ttmp.empty()){
  85. tmp=ttmp.back();
  86. ttmp.pop_back();
  87. v[p[a2]].push_back(tmp);
  88. p[tmp]=p[a2];
  89. }
  90. }
  91. }
  92. }
  93.  
  94. for(int i=0;i<a;i++){
  95. printf("%d:",i);
  96. for(int g=0;g<v[i].size();g++){
  97. printf(" %d",v[i][g]);
  98. }
  99. printf("\n");
  100. }
  101.  
  102. return 0;
  103. }
Runtime error #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Standard output is empty