#C4663. Rotate Matrix - 90° Clockwise In-Place

    ID: 48226 Type: Default 1000ms 256MiB

Rotate Matrix - 90° Clockwise In-Place

Rotate Matrix - 90° Clockwise In-Place

You are given a square matrix \( A \) of size \( n \times n \). Your task is to rotate the matrix by 90° clockwise in-place.

The in-place rotation means you cannot allocate another 2D matrix for the rotation; you must modify the input matrix directly.

The rotation can be achieved by processing the matrix in "layers" and, for each layer, performing cyclic swaps. In particular, for a given layer \( l \) (starting from 0), and for each element index \( i \) in the interval \( [l, n-l-2] \), you swap the following elements:

[ \begin{aligned} &\text{top} ; (l,i) \rightarrow \text{right} ; (i,n-l-1) \ &\text{right} ; (i,n-l-1) \rightarrow \text{bottom} ; (n-l-1,n-i-1) \ &\text{bottom} ; (n-l-1,n-i-1) \rightarrow \text{left} ; (n-i-1,l) \ &\text{left} ; (n-i-1,l) \rightarrow \text{top} ; (l,i) \end{aligned} ]

Input Format: The first line contains an integer \( n \), the size of the matrix. The following \( n \) lines each contain \( n \) space-separated integers representing the matrix rows.

Output Format: Output the rotated matrix. Each line should contain \( n \) space-separated integers representing a row of the rotated matrix.

inputFormat

The first line contains a single integer \( n \) that denotes the size of the matrix.

Then, \( n \) lines follow, each with \( n \) space-separated integers representing the matrix elements.

outputFormat

Output the rotated matrix with each row on a new line and each element separated by a space.

## sample
3
1 2 3
4 5 6
7 8 9
7 4 1

8 5 2 9 6 3

</p>