Grid'y Rotations

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

Consider an M×NM \times N grid of numbers and then a series of (r,c)(r, c) pairs. Each (r,c)(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)(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)(r, c) pair (1,2)(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, MM and NN (4M,N104 \le M, N \le 10), the size of the grid. Then MM lines follow, each having NN numbers. These M×NM \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 QQ (1Q101 \le Q \le 10). Then QQ lines follow, each having rr and cc (1rM21 \le r \le M-2, 1cN21 \le c \le N-2). These QQ lines correspond to the grid rotation you need to perform.

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

Output

For each (r,c)(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

Submit

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.