#K12476. Generate Spiral Matrix

    ID: 23699 Type: Default 1000ms 256MiB

Generate Spiral Matrix

Generate Spiral Matrix

You are given an integer \(n\). Your task is to generate an \(n \times n\) spiral matrix filled with the numbers from 1 to \(n^2\) in a clockwise order. The spiral starts at the top-left corner and moves right, down, left, and up repeatedly until the entire matrix is filled. When \(n = 0\), the matrix is empty.

For example, when \(n = 3\) the matrix will be:

1 2 3
8 9 4
7 6 5

And when \(n = 4\) the matrix will be:

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

inputFormat

A single integer (n) is provided on a single line from standard input.

outputFormat

Output the generated spiral matrix with each row printed on a new line. Elements in a row must be separated by a single space. For (n = 0), output nothing.## sample

3
1 2 3

8 9 4 7 6 5

</p>