#C4225. Spiral Matrix Generation

    ID: 47740 Type: Default 1000ms 256MiB

Spiral Matrix Generation

Spiral Matrix Generation

Given a positive integer ( n ), generate an ( n \times n ) spiral matrix filled with numbers from 1 to ( n^2 ) in clockwise spiral order. The numbers should be arranged such that the first row is filled from left to right, then the last column from top to bottom, then the last row from right to left, and finally the first column from bottom to top, continuing inward until the entire matrix is filled. If ( n ) is not positive (i.e. ( n \le 0 )), the output should be an empty string.

For example, when ( n = 3 ), the resulting matrix is:
1 2 3 8 9 4 7 6 5

When ( n = 4 ), the resulting matrix is:
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7

inputFormat

The input is read from standard input and consists of a single integer ( n ) which indicates the dimensions of the desired spiral matrix. If ( n ) is non-positive, the program should output an empty string.

outputFormat

The output, printed to standard output, is the ( n \times n ) spiral matrix. Each row of the matrix should be printed on a new line, and the numbers in each row are separated by a single space. If ( n ) is non-positive, the output should be empty.## sample

3
1 2 3

8 9 4 7 6 5

</p>