# Grid'y Rotations

Tough Dash, November 2022...
Limits 1s, 512 MB

Consider an $M \times N$ grid of numbers and then a series of $(r, c)$ pairs. Each $(r, c)$ pair corresponds to a position on the grid as the row number and column number. Row and column numbers are all 0-based and start from the top-left corner of the grid.

For each $(r, c)$ pair, in the order they are given, you will have to rotate the eight numbers around that cell in the clockwise direction.

For example, given the following grid:

 1  2  3  4
5  6  7  8
9 10 11 12
13 14 15 16


And the $(r, c)$ pair $(1, 2)$, you have to rotate the eight numbers that are adjacent to 7 in the grid above:

 1  6  2  3
5 10  7  4
9 11 12  8
13 14 15 16


For this problem, you have to print the grid after each rotation.

## Input

The input starts with two integers, $M$ and $N$ ($4 \le M, N \le 10$), the size of the grid. Then $M$ lines follow, each having $N$ numbers. These $M \times N$ numbers form the initial grid. Each number on the grid is guaranteed to fit in a 32-bit signed integer.

The following line will be a single integer $Q$ ($1 \le Q \le 10$). Then $Q$ lines follow, each having $r$ and $c$ ($1 \le r \le M-2$, $1 \le c \le N-2$). These $Q$ lines correspond to the grid rotation you need to perform.

All $(r, c)$ pairs will be valid and correspond to a grid position with eight adjacent numbers.

## Output

For each $(r, c)$ pair, perform the rotation on the grid and print the grid.

## Sample

InputOutput
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2
2 1

1 6 2 3
5 10 7 4
9 11 12 8
13 14 15 16
1 6 2 3
9 5 10 4
13 11 7 8
14 15 12 16


Login to submit.

### Statistics

96% Solution Ratio
user.536969Earliest, 3w ago
FAHIM.ctgFastest, 0.0s
Yasir_ArafatLightest, 5.7 MB
Rahul.003806Shortest, 696B
Toph uses cookies. By continuing you agree to our Cookie Policy.