| “壹零杯”第三届西华师范大学程序设计竞赛 |
|---|
| Finished |
Figure 1: An example of 2048 game grid You are given an $$$N \times N$$$ grid. Each cell in the grid contains either a positive integer of the form $$$2^k\ (k \ge 1)$$$ or $$$0$$$ (representing an empty space).
You need to process an operation string $$$S$$$ of length $$$M$$$, consisting of characters L (Left), R (Right), U (Up), and D (Down). For each character in $$$S$$$, you must slide the numbers in the grid according to the rules of the game 2048.
The rules for sliding and merging are as follows:
Note: Unlike the original game, new tiles are not generated in empty spots after a move.
The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N \le 20, 1 \le M \le 100$$$), representing the size of the grid and the length of the operation string.
The next $$$N$$$ lines each contain $$$N$$$ integers $$$A_{i,j}$$$ ($$$0 \le A_{i,j} \le 1024$$$), representing the initial state of the grid. It is guaranteed that all non-zero integers are powers of 2.
The last line contains a string $$$S$$$ of length $$$M$$$, consisting only of the characters L, R, U, and D.
Output $$$N$$$ lines, each containing $$$N$$$ integers, representing the final state of the grid after all operations.
4 42 2 2 24 2 2 00 4 2 28 0 0 8LRLU
16 0 0 0 8 0 0 0 16 0 0 0 0 0 0 0
Visual explanation of the sample case:
Let the state of the grid be denoted by matrices. The transitions are as follows:
$$$$$$ \underbrace{ \begin{pmatrix} 2 & 2 & 2 & 2 \\ 4 & 2 & 2 & 0 \\ 0 & 4 & 2 & 2 \\ 8 & 0 & 0 & 8 \end{pmatrix} }_{\text{Initial State}} \xrightarrow{(1)\ \textbf{L}} \underbrace{ \begin{pmatrix} 4 & 4 & 0 & 0 \\ 4 & 4 & 0 & 0 \\ 4 & 4 & 0 & 0 \\ 16 & 0 & 0 & 0 \end{pmatrix} }_{\text{After L}} \xrightarrow{(2)\ \textbf{R}} \underbrace{ \begin{pmatrix} 0 & 0 & 0 & 8 \\ 0 & 0 & 0 & 8 \\ 0 & 0 & 0 & 8 \\ 0 & 0 & 0 & 16 \end{pmatrix} }_{\text{After R}} $$$$$$
$$$$$$ \xrightarrow{(3)\ \textbf{L}} \underbrace{ \begin{pmatrix} 8 & 0 & 0 & 0 \\ 8 & 0 & 0 & 0 \\ 8 & 0 & 0 & 0 \\ 16 & 0 & 0 & 0 \end{pmatrix} }_{\text{After L}} \xrightarrow{(4)\ \textbf{U}} \underbrace{ \begin{pmatrix} 16 & 0 & 0 & 0 \\ 8 & 0 & 0 & 0 \\ 16 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix} }_{\text{Final State}} $$$$$$
Transition Details:
| Name |
|---|


