#C2820. Spiral Matrix Generation
Spiral Matrix Generation
Spiral Matrix Generation
Given a non-negative integer \(n\), generate an \(n \times n\) spiral matrix filled with the numbers from 1 to \(n^2\) in clockwise spiral order, starting from the top-left corner. If \(n \leq 0\), the output should be empty (i.e. no output).
The spiral order means that you fill the top row from left to right, then the rightmost column from top to bottom, then the bottom row from right to left and finally the leftmost column from bottom to top, and continue this process for the inner submatrix.
Example 1:
Input: 3</p>Output: 1 2 3 8 9 4 7 6 5
Example 2:
Input: 1</p>Output: 1
inputFormat
The input consists of a single integer \(n\) read from standard input.
\(n\) represents the size of the spiral matrix to be generated.
outputFormat
Output the generated spiral matrix to standard output. Each row of the matrix should be printed on a separate line with each number separated by a single space. If \(n\) is less than or equal to 0, do not output anything.
## sample3
1 2 3
8 9 4
7 6 5
</p>