#P2790. ccj与zrz之积木问题

ccj与zrz之积木问题

Description

From left to right there are nn blocks, numbered from 00 to n1n-1. Simulate the following 4 operations (here aa and bb are block numbers; “return to initial position” means, for example, block 11 goes back to position 11):

  • move a onto b\texttt{move }a\texttt{ onto }b: Return all blocks above aa and above bb to their initial positions, then place aa on top of bb.
  • move a over b\texttt{move }a\texttt{ over }b: Return all blocks above aa to their initial positions, then place aa on the top of the stack containing bb.
  • pile a onto b\texttt{pile }a\texttt{ onto }b: Return all blocks above bb to their initial positions, then move aa together with all blocks above it as a whole onto bb.
  • pile a over b\texttt{pile }a\texttt{ over }b: Move aa together with all blocks above it as a whole onto the top of the stack containing bb.
  • Stop when encountering quit\texttt{quit}. If aa and bb are in the same stack, the instruction is illegal and should be ignored.

Finally, output the list of blocks at each position, in order from bottom to top.

Input Format

The first line contains an integer nn.

Then follow several lines, each containing one instruction (the syntax will be correct). Stop when encountering quit\texttt{quit}.

Output Format

Output exactly nn lines. On the ii-th line, output ii followed by a colon, then a space, then all the blocks at position ii, listed from bottom to top.

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
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:

Hint

Constraints and Notes

For all testdata, 0<n<250<n<25.

Translated by ChatGPT 5