#C1322. Rotate Matrix 90° Clockwise

    ID: 42734 Type: Default 1000ms 256MiB

Rotate Matrix 90° Clockwise

Rotate Matrix 90° Clockwise

You are given an n x n matrix. Your task is to rotate the matrix 90° clockwise in place. In other words, you should modify the original matrix without using additional space for another matrix.

The typical approach involves two steps:

  1. Transpose the matrix (swap rows with columns).
  2. Reverse each row of the transposed matrix.

For example, if the input matrix is:

1 2
3 4

After rotating, it becomes:

3 1
4 2

Apply the same method for larger matrices.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains a single integer n, the size of the matrix.
  • The following n lines each contain n space-separated integers representing a row of the matrix.

outputFormat

Output the rotated matrix to stdout in the following format:

  • There should be n lines, each containing n space-separated integers that represent the rows of the matrix after the rotation.
## sample
2
1 2
3 4
3 1

4 2

</p>