Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Shifting a Matrix

You are given $N \times N$ matrix $A$ initialized with $A_{i,j} = (i - 1)N + j$, where $A_{i,j}$ is the entry of the $i$-th row and the $j$-th column of $A$. Note that $i$ and $j$ are 1-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 $i$: circular shift of the $i$-th row to the left, i.e., setting previous $A_{i,k}$ to new $A_{i,k-1}$ for $2 \leq k \leq N$, and previous $A_{i,1}$ to new $A_{i,N}$.

Right shift with $i$: circular shift of the $i$-th row to the right, i.e., setting previous $A_{i,k}$ to new $A_{i,k+1}$ for $1 \leq k \leq N - 1$, and previous $A_{i,N}$ to new $A_{i,1}$.

Up shift with $j$: circular shift of the $j$-th column to the above, i.e., setting previous $A_{k,j}$ to new $A_{k-1,j}$ for $2 \leq k \leq N$, and previous $A_{1,j}$ to new $A_{N,j}$.

Down shift with $j$: circular shift of the $j$-th column to the below, i.e., setting previous $A_{k,j}$ to new $A_{k+1,j}$ for $1 \leq k \leq N - 1$, and previous $A_{N,j}$ to new $A_{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 $m$ times, where $m$ 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

3 2
R1

Output for the Sample Input 1

3 1 2
4 5 6
7 8 9

Sample Input 2

3 7
(U2)300

Output for the Sample Input 2

1 2 3
4 5 6
7 8 9

Sample Input 3

3 7
(R1D1)3

Output for the Sample Input 3

3 4 7
1 5 6
2 8 9

Solution in C (judge verdict: Accepted)

// AOJ 2731 Shifting a Matrix
// 2018.2.11 bal4u

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define SIZE 700

int N;
typedef struct { short r, c; } T;
T ans[101][101], t0[101][101];
T tbl[SIZE+1][101][101];
char used[SIZE+1]; int top;
char s[1002], *p;

char *seq(T res[101][101], char *s);

// malloc
int new() { int i, k;
	used[k=i=top] = 1;
	while (++i < SIZE) if (!used[i]) { top = i; break; }
	return k;
}

// free
void dis(int k) { used[k] = 0; if (k < top) top = k; }

char *in(int *res, char *p) { int n = 0;
	while (isdigit(*p)) n = (n<<3)+(n<<1) + (*p++ & 0xf); *res = n; return p;
}

void calc(T a[101][101], T b[101][101])
{
	int r, c;
	T t[101][101];

	for (r = 0; r < N; r++) for (c = 0; c < N; c++) {
		t[r][c] = a[b[r][c].r][b[r][c].c];
	}
	memcpy(a, t, sizeof(t));
}

char *ope(T a[101][101], char *s)
{
	int k, r, c;
	char x;

	x = *s++;
	s = in(&k, s), k--;
	memcpy(a, t0, sizeof(t0));

	if      (x == 'L') { for (c = 0; c < N; c++) a[k][c].c = c+1; a[k][N-1].c = 0; }
	else if (x == 'R') { for (c = 0; c < N; c++) a[k][c].c = c-1; a[k][0].c = N-1; }
	else if (x == 'U') { for (r = 0; r < N; r++) a[r][k].r = r+1; a[N-1][k].r = 0; }
	else               { for (r = 0; r < N; r++) a[r][k].r = r-1; a[0][k].r = N-1; }
	return s;
}

char *rep(T a[101][101], char *s)
{
	int n, _b;
	T (*b)[101];
	
	b = tbl[_b=new()];
	s = seq(b, s)+1;
	s = in(&n, s);
	memcpy(a, t0, sizeof(t0));

	while (n) {
		if (n & 1) calc(a, b);
		calc(b, b);
		n >>= 1;
	}
	dis(_b);
	return s;
}

char *seq(T res[101][101], char *s)
{
	int _t;
	T (*t)[101];

	memcpy(res, t0, sizeof(t0));
	t = tbl[_t=new()];
	while (*s == '(' || isalpha(*s)) {
		if (*s == '(') s = rep(t, s + 1);
		else           s = ope(t, s);
		calc(res, t);
	}
	dis(_t);
	return s;
}

int main()
{
	int L, r, c;
	
	fgets(p=s, 16, stdin), p = in(&N, p), in(&L, p+1);
	fgets(s, 1002, stdin);

	for (r = 0; r < N; r++) for (c = 0; c < N; c++)
		t0[r][c].r = r, t0[r][c].c = c;

	seq(ans, s);
	for (r = 0; r < N; r++) {
		printf("%d", N*ans[r][0].r + ans[r][0].c + 1);
		for (c = 1; c < N; c++)
			printf(" %d", N*ans[r][c].r + ans[r][c].c + 1);
		putchar('\n');
	}
	return 0;
}