#C11332. Spiral Matrix Generation

    ID: 40637 Type: Default 1000ms 256MiB

Spiral Matrix Generation

Spiral Matrix Generation

This problem requires you to generate an n x m spiral matrix filled with consecutive integers starting from 1. The numbers should be arranged in a spiral order (clockwise) starting from the top-left corner.

The spiral order is defined as follows:

  • Fill the first row from the left to right.
  • Fill the last column from top to bottom.
  • Fill the last row from right to left.
  • Fill the first column from bottom to top.

This process continues by moving inwards until all the cells are filled. If either dimension is non-positive, the output should be empty.

Note: For any invalid grid size (n ≤ 0 or m ≤ 0), output nothing.

inputFormat

The input consists of two space-separated integers n and m in a single line, representing the number of rows and columns of the matrix respectively.

For example:

3 3

outputFormat

Print the n x m spiral matrix where each row is printed on a new line and the numbers in a row are separated by a single space.

If the matrix is empty (i.e., n ≤ 0 or m ≤ 0), output nothing.

For example, for the input 3 3, the output should be:

1 2 3
8 9 4
7 6 5
## sample
3 3
1 2 3

8 9 4 7 6 5

</p>