Little Artem and Matrix (Codeforces 668A)
Tags: Implement, implementation

Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.

Input: The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively.Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m,  - 109 ≤ xi ≤ 109) are given.Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.

Output: Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.If there are multiple valid solutions, output any of them.


Sample Inputs:
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8

3 3 2
1 2
3 2 2 5

Sample Outputs:
8 2
1 8

0 0 0
0 0 5
0 0 0

Solution (Python):
f = lambda : map(int, input().split())
(n, m, q) = f()
p = [[0] * m for j in range(n)]
for t in [list(f()) for k in range(q)][::-1]:
    j = t[1] - 1
    if t[0] == 1:
        p[j].insert(0, p[j].pop())
    elif t[0] == 2:
        s = p[-1][j]
        for i in range(n - 1, 0, -1):
            p[i][j] = p[i - 1][j]
        p[0][j] = s
    else:
        p[j][t[2] - 1] = t[3]
for d in p:
    print(*d)

Solution (Python):
(n, m, q) = map(int, input().split())
res = [0] * int(10000.0 + 5)
arr = list(([0] * (m + 1) for _ in range(n + 1)))

def shift_row(r):
    arr[r][m] = arr[r][m - 1]
    for i in range(m - 1, 0, -1):
        arr[r][i] = arr[r][i - 1]
    arr[r][0] = arr[r][m]

def shift_col(c):
    arr[n][c] = arr[n - 1][c]
    for i in range(n - 1, 0, -1):
        arr[i][c] = arr[i - 1][c]
    arr[0][c] = arr[n][c]
for i in range(q):
    qi = list(map(int, input().split()))
    if qi[0] == 3:
        res[i] = ((qi[0], qi[3]), (qi[1], qi[2]))
    else:
        res[i] = ((qi[0], qi[1]), (-1, -1))
for i in range(q - 1, -1, -1):
    if res[i][0][0] == 3:
        arr[res[i][1][0] - 1][res[i][1][1] - 1] = res[i][0][1]
    elif res[i][0][0] == 1:
        shift_row(res[i][0][1] - 1)
    elif res[i][0][0] == 2:
        shift_col(res[i][0][1] - 1)
for i in range(n):
    print(*arr[i][:-1])

Solution (Python):
class CodeforcesTask668ASolution:

    def __init__(self):
        self.result = ''
        self.n_m_q = []
        self.experiment = []

    def read_input(self):
        self.n_m_q = [int(x) for x in input().split(' ')]
        for x in range(self.n_m_q[2]):
            self.experiment.append([int(y) for y in input().split(' ')])

    def process_task(self):
        try:
            construction = self.experiment[::-1]
            matrix = [[0] * self.n_m_q[1] for x in range(self.n_m_q[0])]
            for con in construction:
                if con[0] == 1:
                    tmp = matrix[con[1] - 1][-1]
                    for x in range(self.n_m_q[1]):
                        (tmp, matrix[con[1] - 1][x]) = (matrix[con[1] - 1][x], tmp)
                elif con[0] == 2:
                    tmp = matrix[-1][con[1] - 1]
                    for x in range(self.n_m_q[0]):
                        (tmp, matrix[x][con[1] - 1]) = (matrix[x][con[1] - 1], tmp)
                elif con[0] == 3:
                    matrix[con[1] - 1][con[2] - 1] = con[3]
            self.result = '\n'.join([' '.join([str(x) for x in row]) for row in matrix])
        except Exception as e:
            print(e, con, matrix)

    def get_result(self):
        return self.result
if __name__ == '__main__':
    Solution = CodeforcesTask668ASolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())