cswork / OAOpen practice workspace

Capital OneOA #18

Rotate Matrix Over Diagonals

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationIn-place Arrays

You are given a non-empty n×n square matrix and a non-negative count turns. In each turn, the elements on both main diagonals stay in place, and the remaining four triangular regions rotate 90 degrees clockwise: a non-diagonal element at original coordinate (r,c) moves to (c,n−1−r), with coordinates counted from 0. Output the matrix after repeating this turns times. This mapping matches the fixed original 5×5 example entry by entry; the public source image on the original page explicitly shows a clockwise rotation with both diagonals fixed.

Input

The first line contains n turns, followed by n lines of n integers each, separated by whitespace; LF/CRLF line endings and a trailing EOF are accepted. The original gave no numeric upper bounds; this site publicly adapts them to Java types: n≥1 (a non-empty square matrix), elements in −2147483648..2147483647, and turns 0..2147483647. n is limited only by the existing 32MiB input budget, with no separate smaller cap; 4095 is derived from the shortest-encoding budget and is not a source constraint. Empty matrices and negative turn counts are not supported as extensions.

Output

Output the rotated matrix as n lines of n integers each; do not output n.

Example 1

Input:

5 1
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Output:

1 16 11 6 5
22 7 12 9 2
23 18 13 8 3
24 17 14 19 4
21 20 15 10 25

Example 2

Input:

2 2147483647
1 2
3 4

Output:

1 2
3 4

Example 3

Input:

1 0
-2147483648

Output:

-2147483648

Example notes

The first public example keeps the original complete 5×5 input and output; both diagonals, 1/7/13/19/25 and 5/9/13/17/21, stay fixed. a 2×2 matrix is entirely diagonal and stays unchanged after any number of turns; a 1×1 matrix and zero turns likewise leave the matrix unchanged. The source image was downloaded separately and bound to its own hash.

Editorial

Loading solution…