Back
Close

Rotating a square matrix

Statement
Rotate the [[S]] x [[S]] square matrix by the given amount and change numbers like this: Decrease the maximum elements and increase the the minimum elements by 1 for each 90° rotation step. Rotation direction: Clockwise for rotations > 0 and counterclockwise for rotations < 0.

Input description
<<Line 1:>> Two space separated integers [[R]] for the amount of rotation to be applied and [[S]] for the matrix size. <<Next [[N]] lines:>> Each line [[L]] of the matrix with numbers separated by space.

Output description
Lines with space separated numbers of the rotated matrix with numbers changed at each 90° step.

Constraints
-720 < degrees rotation <= 720 2 <= matrix size <= 5

Game modes

Test cases
Test 1 Test
Input
-90 2 1 2 3 4
Output
2 3 2 3

Validator 1 Validator
Input
-90 2 3 4 1 2
Output
3 2 3 2

Test 2 Test
Input
90 3 0 2 3 4 5 6 7 8 10
Output
7 4 1 8 5 2 9 6 3

Validator 2 Validator
Input
90 3 7 8 9 4 5 6 1 2 3
Output
2 4 7 2 5 8 3 6 8

Test 3 Test
Input
-180 3 -1 2 3 4 5 6 7 8 11
Output
9 8 7 6 5 4 3 2 1

Validator 3 Validator
Input
-180 3 7 8 9 4 5 6 1 2 3
Output
3 3 3 6 5 4 7 7 7

Test 4 Test
Input
270 4 4 8 2 1 9 4 1 8 4 1 2 7 1 6 5 3
Output
4 6 6 4 4 4 4 5 6 4 4 6 4 6 4 4

Validator 4 Validator
Input
270 4 1 6 5 3 9 4 1 8 4 1 2 7 4 8 2 1
Output
4 6 6 4 5 4 4 4 6 4 4 6 4 6 4 4

Test 5 Test
Input
630 4 14 18 22 11 19 14 11 18 4 11 12 17 11 6 15 13
Output
11 15 15 13 15 11 12 15 15 14 11 11 14 15 11 11

Validator 5 Validator
Input
720 4 11 6 15 13 19 14 11 18 4 11 12 17 14 18 22 11
Output
12 12 14 13 14 14 12 14 12 12 12 14 14 14 14 12

Test 6 Test
Input
-720 5 14 18 22 11 59 19 14 11 18 23 4 11 12 17 12 11 6 15 13 4 -12 -11 -5 -2 -9
Output
14 18 22 11 51 19 14 11 18 23 4 11 12 17 12 11 6 15 13 4 -4 -4 -4 -2 -4

Validator 6 Validator
Input
-720 5 -12 -11 -5 -2 -9 19 14 11 18 23 4 11 12 17 12 11 6 15 13 4 14 18 22 11 59
Output
-4 -4 -4 -2 -4 19 14 11 18 23 4 11 12 17 12 11 6 15 13 4 14 18 22 11 51

Solution language

Solution

Stub generator input