Under construction

Still being built. Things may change

Triangular Key

Interview

Time limit2sMemory limit1024 MB

Summary
Rotate a triangular grid of digits 90 degrees in the given direction and print the result in the same triangular format.
Level

Medium4 of 10

Topics
Implementation, Matrix
Solved
No attempts yet

Problem

Indiana Jones has a key to the door that leads to the Inca's hidden treasure. The key is a regular triangle divided into n2n^2 small regular triangles, and each small triangle has one decimal digit written in it. The key is shown in the figure below.

Jones fit the triangle into the matching triangular hole in the door, and the door did not open. He may have inserted the key incorrectly, since it can be inserted in three ways. Jones has only one attempt left. If he fails, the Inca warriors chasing him will kill him. He wants to see how the key looks when inserted the other two ways, and then he will choose the right way. You are one of his two assistants, and he asks you to show him only one of the two possible variants. The key above, rotated counterclockwise and clockwise, is shown in the figure below.

If you want Indiana to survive and want your share of the treasure, you have to help him!

Input

The first line contains one integer nn (1≤n≤1001 \le n \le 100). The next nn lines describe the triangular key. Line i+1i + 1 contains 2i−12 i - 1 digits, which are the contents of the ii-th row of the key.

The last line contains the word counterclockwise if the triangle must be rotated counterclockwise, and clockwise otherwise.

Output

Print the key after rotating it in the required direction. Describe the triangle in the same format as the input.

Examples2

  1. Example 1

    Input
    3
    1 
    2 3 4
    5 6 7 8 9
    counterclockwise
    
    Expected output
    9
    4 8 7
    1 3 2 6 5
    
  2. Example 2

    Input
    3
    1 
    2 3 4
    5 6 7 8 9
    clockwise
    
    Expected output
    5
    7 6 2
    9 8 4 3 1