All problems

Problem 37

Rotate Matrix

Medium · 20 pts · Array, Matrix

Problem

You are given a square matrix with N rows and N columns. Rotate it 90 degrees clockwise and print the result. After a clockwise rotation, the first column of the original, read from bottom to top, becomes the first row; the second column, read from bottom to top, becomes the second row, and so on. In other words, the new element at row i, column j is the old element at row N - 1 - j, column i. An in-place method is to transpose the matrix and then reverse every row.

Input

First line: integer N. Next N lines: N space-separated integers each.

Output

Print the rotated matrix: N lines with N space-separated integers.

Constraints

1 <= N <= 100; -1000 <= value <= 1000

Example input

3
1 2 3
4 5 6
7 8 9

Example output

7 4 1
8 5 2
9 6 3

Explanation

For 1 2 3 / 4 5 6 / 7 8 9, the first column read upwards is 7 4 1, which becomes the first row. The other columns give 8 5 2 and 9 6 3, so the result is 7 4 1 / 8 5 2 / 9 6 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Spiral Matrix Next: Transpose Matrix