#C359. Grid Shifting Operations

    ID: 47033 Type: Default 1000ms 256MiB

Grid Shifting Operations

Grid Shifting Operations

You are given an m x n grid of integers and a list of shift commands. Each command specifies a direction, an index, and an amount by which to shift a row or column. The four possible commands are:

  • R i k: Shift row i to the right by k positions.
  • L i k: Shift row i to the left by k positions.
  • U j k: Shift column j upward by k positions.
  • D j k: Shift column j downward by k positions.

All shifts are cyclic; that is, elements shifted off one end reappear at the other end.

The shifting operations can be described mathematically. For example, shifting a row i to the right by k positions is equivalent to transforming the row, \(r = [a_0, a_1, \dots, a_{n-1}]\), into \[ a'_j = a_{(j - k) \bmod n}, \quad 0 \leq j < n. \]

Your task is to apply all given commands on the grid sequentially and print the final grid.

inputFormat

The input is read from standard input and has the following format:

  1. The first line contains two integers, m and n, representing the number of rows and columns.
  2. The next m lines each contain n space-separated integers, representing the grid.
  3. The following line contains a single integer t, the number of commands.
  4. The next t lines each contain a command in the format described above.

outputFormat

Output the final grid to standard output. Each of the m lines should contain n space-separated integers representing a row of the grid.

## sample
3 3
1 2 3
4 5 6
7 8 9
1
R 0 1
3 1 2

4 5 6 7 8 9

</p>