#C359. Grid Shifting Operations
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:
- The first line contains two integers,
m
andn
, representing the number of rows and columns. - The next
m
lines each containn
space-separated integers, representing the grid. - The following line contains a single integer
t
, the number of commands. - 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.
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>