#C7435. Spiral Matrix
Spiral Matrix
Spiral Matrix
Given a non-negative integer \(n\), generate a spiral matrix of size \(n \times n\) filled with consecutive integers from 1 to \(n^2\). The matrix should be filled in a clockwise spiral order starting from the top-left corner.
For example:
- For \(n = 0\), the output is
[]
. - For \(n = 1\), the output is
[[1]]
. - For \(n = 2\), the output is
[[1, 2], [4, 3]]
. - For \(n = 3\), the output is
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
. - For \(n = 4\), the output is
[[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]
.
Your task is to implement a program that reads \(n\) from standard input and prints the spiral matrix in the nested list format exactly as shown above.
inputFormat
The input consists of a single non-negative integer \(n\) read from standard input.
outputFormat
Output the spiral matrix in nested list format. For \(n = 0\), output []
. Otherwise, output the entire nested list representation where each row is printed in Python list format.
0
[]