#icpc2015autumne. [icpc2015autumn_e]Shifting a Matrix

[icpc2015autumn_e]Shifting a Matrix

MathJax.Hub.Config({ tex2jax: { inlineMath: [["","",""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }

Problem Statement

You are given NtimesNN \\times N matrix AA initialized with A_i,j=(i1)N+jA\_{i,j} = (i-1)N + j, where A_i,jA\_{i,j} is the entry of the ii-th row and the jj-th column of AA. Note that ii and jj are 11-based.

You are also given an operation sequence which consists of the four types of shift operations: left, right, up, and down shifts. More precisely, these operations are defined as follows:

  • Left shift with ii: circular shift of the ii-th row to the left, i.e., setting previous A_i,kA\_{i,k} to new A_i,k1A\_{i,k-1} for 2leqkleqN2 \\leq k \\leq N, and previous A_i,1A\_{i,1} to new A_i,NA\_{i,N}.

  • Right shift with ii: circular shift of the ii-th row to the right, i.e., setting previous A_i,kA\_{i,k} to new A_i,k+1A\_{i,k+1} for 1leqkleqN11 \\leq k \\leq N-1, and previous A_i,NA\_{i,N} to new A_i,1A\_{i,1}.

  • Up shift with jj: circular shift of the jj-th column to the above, i.e., setting previous A_k,jA\_{k,j} to new A_k1,jA\_{k-1,j} for 2leqkleqN2 \\leq k \\leq N, and previous A_1,jA\_{1,j} to new A_N,jA\_{N,j}.

  • Down shift with jj: circular shift of the jj-th column to the below, i.e., setting previous A_k,jA\_{k,j} to new A_k+1,jA\_{k+1,j} for 1leqkleqN11 \\leq k \\leq N-1, and previous A_N,jA\_{N,j} to new A_1,jA\_{1,j}.

An operation sequence is given as a string. You have to apply operations to a given matrix from left to right in a given string. Left, right, up, and down shifts are referred as 'L', 'R', 'U', and 'D' respectively in a string, and the following number indicates the row/column to be shifted. For example, "R25" means we should perform right shift with 25. In addition, the notion supports repetition of operation sequences. An operation sequence surrounded by a pair of parentheses must be repeated exactly mm times, where mm is the number following the close parenthesis. For example, "(L1R2)10" means we should repeat exactly 10 times the set of the two operations: left shift with 1 and right shift with 2 in this order.

Given operation sequences are guaranteed to follow the following BNF:

<sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation>
<repetition> := '('<sequence>')'<number>
<operation> := <shift><number>
<shift> := 'L' | 'R' | 'U' | 'D'
<number> := <nonzero_digit> |<number><digit>
<digit> := '0' | <nonzero_digit>
<nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'```

Given $N$ and an operation sequence as a string, make a program to compute the $N \\times N$ matrix after operations indicated by the operation sequence.

* * *

### Input

The input consists of a single test case. The test case is formatted as follows.

> $N$ $L$  
> $S$

The first line contains two integers $N$ and $L$, where $N$ ($1 \\leq N \\leq 100$) is the size of the given matrix and $L$ ($2 \\leq L \\leq 1{,}000$) is the length of the following string. The second line contains a string $S$ representing the given operation sequence. You can assume that $S$ follows the above BNF. You can also assume numbers representing rows and columns are no less than $1$ and no more than $N$, and the number of each repetition is no less than $1$ and no more than $10^9$ in the given string.

### Output

Output the matrix after the operations in $N$ lines, where the $i$-th line contains single-space separated $N$ integers representing the $i$-th row of $A$ after the operations.

* * *

* * *

### Sample Input 1

```plain
3 2
R1```

### Output for the Sample Input 1

```plain
3 1 2
4 5 6
7 8 9```

* * *

### Sample Input 2

```plain
3 7
(U2)300```

### Output for the Sample Input 2

```plain
1 2 3
4 5 6
7 8 9```

* * *

### Sample Input 3

```plain
3 7
(R1D1)3```

### Output for the Sample Input 3

```plain
3 4 7
1 5 6
2 8 9```

* * *