101


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 3 p.m.

FORMAT: Text only

SIZE: 3.4 kB

HITS: 2053

  1. #include <cstdio>
  2. int blocks[25][26];
  3. int n;
  4. void find( const int & val, int & y, int & x )
  5. {
  6. bool loop = true;
  7. for ( int i = 0; i < n && loop; ++i )
  8. for ( int j = 0; blocks[i][j] != -1; ++j )
  9. if ( blocks[i][j] == val )
  10. {
  11. y = i;
  12. x = j;
  13. loop = false;
  14. break;
  15. }
  16. }
  17. void move_onto( const int & a, const int & b )
  18. {
  19. int y1, x1, y2, x2;
  20. find( a, y1, x1 );
  21. find( b, y2, x2 );
  22. if ( y1 == y2 )
  23. return ;
  24. // make sure nothing is on a
  25. for ( int x = x1+1; blocks[y1][x] != -1; ++x )
  26. {
  27. blocks[ blocks[y1][x] ][0] = blocks[y1][x];
  28. blocks[y1][x] = -1;
  29. }
  30. // make sure nothing is on b
  31. for ( int x = x2+1; blocks[y2][x] != -1; ++x )
  32. {
  33. blocks[ blocks[y2][x] ][0] = blocks[y2][x];
  34. blocks[y2][x] = -1;
  35. }
  36. // put a on b
  37. blocks[y2][x2+1] = blocks[y1][x1];
  38. blocks[y2][x2+2] = -1;
  39. blocks[y1][x1] = -1;
  40. }
  41. void move_over( const int & a, const int & b )
  42. {
  43. int y1, x1, y2, x2;
  44. find( a, y1, x1 );
  45. find( b, y2, x2 );
  46. if ( y1 == y2 )
  47. return ;
  48. // make sure nothing is on a
  49. for ( int x = x1+1; blocks[y1][x] != -1; ++x )
  50. {
  51. blocks[ blocks[y1][x] ][0] = blocks[y1][x];
  52. blocks[y1][x] = -1;
  53. }
  54. // find top of stack which contains b
  55. for ( ; blocks[y2][x2] != -1; ++x2 );
  56. blocks[y2][x2] = blocks[y1][x1];
  57. blocks[y2][x2+1] = -1;
  58. blocks[y1][x1] = -1;
  59. }
  60. void pile_onto( const int & a, const int & b )
  61. {
  62. int y1, x1, y2, x2;
  63. find( a, y1, x1 );
  64. find( b, y2, x2 );
  65. if ( y1 == y2 )
  66. return ;
  67. // make sure nothing is on b
  68. for ( int x = x2+1; blocks[y2][x] != -1; ++x )
  69. {
  70. blocks[ blocks[y2][x] ][0] = blocks[y2][x];
  71. blocks[y2][x] = -1;
  72. }
  73. // move a and above
  74. ++x2;
  75. for ( ; blocks[y1][x1] != -1; ++x1, ++x2 )
  76. {
  77. blocks[y2][x2] = blocks[y1][x1];
  78. blocks[y1][x1] = -1;
  79. }
  80. blocks[y2][x2] = -1;
  81. }
  82. void pile_over( const int & a, const int & b )
  83. {
  84. int y1, x1, y2, x2;
  85. find( a, y1, x1 );
  86. find( b, y2, x2 );
  87. if ( y1 == y2 )
  88. return ;
  89. // find top of stack which contains b
  90. for ( ; blocks[y2][x2] != -1; ++x2 );
  91. // move a and above
  92. for ( ; blocks[y1][x1] != -1; ++x1, ++x2 )
  93. {
  94. blocks[y2][x2] = blocks[y1][x1];
  95. blocks[y1][x1] = -1;
  96. }
  97. blocks[y2][x2] = -1;
  98. }
  99. void print( )
  100. {
  101. for ( int i = 0; i < n; ++i )
  102. {
  103. printf("%d:", i);
  104. for ( int j = 0; blocks[i][j] != -1; ++j )
  105. printf(" %d", blocks[i][j]);
  106. printf("\n");
  107. }
  108. }
  109. int main()
  110. {
  111. int a, b;
  112. char mopi[5], onov[5];
  113. scanf("%d\n", &n);
  114. for ( int i = 0; i < n; ++i )
  115. {
  116. blocks[i][0] = i;
  117. blocks[i][1] = -1;
  118. }
  119. while ( scanf("%s", mopi) == 1 && mopi[0] != 'q' )
  120. {
  121. scanf("%d %s %d", &a, onov, &b);
  122. if ( mopi[0] == 'm' && onov[3] == 'o' )
  123. move_onto( a, b );
  124. else if ( mopi[0] == 'm' && onov[3] == 'r' )
  125. move_over( a, b );
  126. else if ( mopi[0] == 'p' && onov[3] == 'o' )
  127. pile_onto( a, b );
  128. else if ( mopi[0] == 'p' && onov[3] == 'r' )
  129. pile_over( a, b );
  130. }
  131. print( );
  132. return 0;
  133. }

comments powered by Disqus