Hide

Problem E
Sliding Tiles

You have placed 25 lettered tiles in a nice, neat $5 \times 5$ grid like the first one below. You start to slide the tiles around one space at a time, either up, down, left or right. As you move a tile, it pushes along any tiles ahead of it. For example, if you push the M to the right, then push the X up, V to the right and O to the left, your nice neat grid would end up looking like the second picture. Your job is to write a simulation of this process.

\includegraphics[width=0.4\textwidth ]{tile1} \includegraphics[width=0.4\textwidth ]{tile2}

Input

Input consists of up to $100$ test cases. At the start of each test case, the tiles are reset to the $5 \times 5$ grid in the first picture above. Input for each test case starts with a line containing $0 \le n \le 50$. This is followed by $n$ lines, each describing a move of a tile. A move is of the form $x$ up, $x$ down, $x$ left or $x$ right, where $x$ is a capital letter from A to Y. The end of all test cases is marked with a value of $-1$ for $n$.

Output

For each test case, produce a picture of the state of the board after all moves have completed. The topmost tile should appear on the first row and the bottommost should appear on the last. Similarly, the leftmost tile should be on the first column and the rightmost on the last. Some rows may be empty, but don’t use any extra spaces at the end of any row. Follow each test case with one blank row.

Sample Input 1 Sample Output 1
4
V up
V up
G right
G down
4
E left
B down
P right
X up
-1
 B

ALGDE
FQCIJ
KVHNO
P MST
U RXY
  W

A CDEJ
 BGHIO
 FLMNT
 KQRSX
  PVW Y
 U

Please log in to submit a solution to this problem

Log in