#C1816. Spiral Matrix Generation
Spiral Matrix Generation
Spiral Matrix Generation
You are given an integer (n). Your task is to generate an (n \times n) spiral matrix filled with integers from 1 to (n^2) in a spiral order. The filling starts from the top-left cell and proceeds rightwards, then downwards, leftwards, and upwards in a loop until the entire matrix is filled. The process can be described as follows:
- Initialize four boundaries: top = 0, bottom = n-1, left = 0, and right = n-1.
- Fill the top row from the left boundary to the right boundary.
- Fill the right column from the updated top boundary to the bottom boundary.
- If there are remaining rows, fill the bottom row from the right boundary to the left boundary.
- If there are remaining columns, fill the left column from the bottom boundary to the top boundary.
- Update the boundaries and repeat until the matrix is completely filled.
The matrix must satisfy (1 \leq M[i][j] \leq n^2) and maintain the spiral ordering. Good luck!
inputFormat
The input consists of a single integer (n) ((1 \leq n \leq 100)).
outputFormat
Output the generated spiral matrix. Each row should be printed on a separate line, with values separated by a single space.## sample
1
1