#C11105. Spiral Pattern of Marbles

    ID: 40385 Type: Default 1000ms 256MiB

Spiral Pattern of Marbles

Spiral Pattern of Marbles

In this problem, you are given a square grid of side length (n) and a sequence of (n^2) unique positive integers representing marble identifiers. Your task is to arrange these marbles in a spiral pattern starting from the top-left corner, moving right, then down, left, and up repeatedly until the grid is filled. The resulting grid should be printed row by row, with each number separated by a single space. This problem tests your ability to manage two-dimensional arrays and simulate spiral order traversal.

inputFormat

The first line of input contains an integer (T) denoting the number of test cases. For each test case, the first number is an integer (n) which denotes the side length of the grid. It is followed by (n^2) space-separated integers representing the marble identifiers.

outputFormat

For each test case, output the resulting (n \times n) grid arranged in a spiral pattern. Each row should be printed on a separate line with the elements separated by a single space. Separate the output for different test cases with an empty line.## sample

2
3 1 2 3 4 5 6 7 8 9
4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3

8 9 4 7 6 5

1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7

</p>