#P2790. The Blocks Problem Simulation

    ID: 16052 Type: Default 1000ms 256MiB

The Blocks Problem Simulation

The Blocks Problem Simulation

You are given n blocks numbered from 0 to n-1 placed in a row such that block i starts at position i. Your task is to simulate the following commands:

  • \(move\ a\ onto\ b\): Return any blocks that are on top of both block \(a\) and block \(b\) to their original positions, then move block \(a\) so that it sits directly on top of block \(b\).
  • \(move\ a\ over\ b\): Return any blocks that are on top of block \(a\) to their original positions, then move block \(a\) onto the top of the pile containing block \(b\).
  • \(pile\ a\ onto\ b\): Return any blocks that are on top of block \(b\) to their original positions, then move the pile of blocks consisting of block \(a\) and any blocks above it so that they are placed on top of block \(b\) (keeping their order).
  • \(pile\ a\ over\ b\): Move the pile of blocks consisting of block \(a\) and any blocks above it onto the top of the pile containing block \(b\) (keeping their order).

If blocks \(a\) and \(b\) are in the same pile, the command is illegal and must be ignored. The input will end with the command quit.

After processing all commands, output the final configuration. For each position from 0 to n-1, print the list of blocks at that position from bottom to top.

inputFormat

The input begins with a single integer n (the number of blocks). Each subsequent line contains one of the following commands:

  • move a onto b
  • move a over b
  • pile a onto b
  • pile a over b

Commands are given one per line and the sequence ends with the command quit.

outputFormat

For each position from 0 to n-1, output a line in the following format:

i: block1 block2 ...

where i is the position number and the blocks are listed from bottom to top. If a position has no blocks, simply print the position number followed by a colon.

sample

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
quit
0: 0

1: 1 9 2: 2 3: 3 4: 4 5: 5 8 7 6 6: 7: 8: 9:

</p>