#C1816. Spiral Matrix Generation

    ID: 45063 Type: Default 1000ms 256MiB

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:

  1. Initialize four boundaries: top = 0, bottom = n-1, left = 0, and right = n-1.
  2. Fill the top row from the left boundary to the right boundary.
  3. Fill the right column from the updated top boundary to the bottom boundary.
  4. If there are remaining rows, fill the bottom row from the right boundary to the left boundary.
  5. If there are remaining columns, fill the left column from the bottom boundary to the top boundary.
  6. 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