Consider an grid of numbers and then a series of pairs. Each 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 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 pair , 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.
The input starts with two integers, and (), the size of the grid. Then lines follow, each having numbers. These 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 (). Then lines follow, each having and (, ). These lines correspond to the grid rotation you need to perform.
All pairs will be valid and correspond to a grid position with eight adjacent numbers.
For each pair, perform the rotation on the grid and print the grid.
Input | Output |
---|---|
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 |