problems / 176

Suki's Garden Rotation

easy matrix arrays simulation 1000 ms · 256 MB

Suki's Garden Rotation

Suki has a special garden laid out in an n×n grid. Each cell contains a flower with a particular bloom pattern. One morning, Suki's owner decides to rotate the entire garden 90° clockwise to catch more of the afternoon sun. The flowers must be rearranged so that the leftmost column becomes the top row, the bottom row becomes the leftmost column, and so on.

Given the original garden layout as an n×n grid, output the rotated layout after a 90° clockwise rotation.

Input

The first line contains n (1 ≤ n ≤ 500), the size of the garden grid.

The next n lines each contain n integers a_{i,j} (0 ≤ a_{i,j} ≤ 10^9), representing the bloom pattern ID at position (i, j) in the original garden.

Output

Print n lines, each containing n space-separated integers. The output should represent the rotated garden.

Notes

For a 90° clockwise rotation, the element at position (i, j) in the original grid should move to position (j, n−1−i) in the rotated grid. Equivalently, the top-left to bottom-right diagonal becomes the top-left to top-right row, the left column becomes the top row, and the top row becomes the right column.

Example 1

Input

2
1 2
3 4

Output

3 1
4 2

Example 2

Input

3
1 2 3
4 5 6
7 8 9

Output

7 4 1
8 5 2
9 6 3

Hints · reveal one at a time if you're stuck

Hint 1

Work out on a 2 by 2 or 3 by 3 grid where the element at position (i, j) lands after a 90 degree clockwise turn. Which original row and column give the first row of the result?

Hint 2

A clockwise rotation can be seen as two simpler steps on the grid, such as flipping across the main diagonal and then reversing each row. Print each row with space-separated values.

report an issue Prefer your own editor? sooks prep 176 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.