#K42892. Zigzag Matrix Pattern

    ID: 27188 Type: Default 1000ms 256MiB

Zigzag Matrix Pattern

Zigzag Matrix Pattern

You are given an integer (D) on the first line representing the number of matrices to generate. Each of the following (D) lines contains an integer (n) indicating the dimension of a square matrix. For each matrix, fill it with consecutive integers starting from 1 in a zigzag pattern: fill the first row from left to right, the second row from right to left, and continue alternating the direction for every subsequent row. Finally, print each matrix with its rows on separate lines and leave an empty line between consecutive matrices.

inputFormat

The input is given via standard input (stdin). The first line contains a single integer (D) ((1 \leq D \leq 100)). The next (D) lines each contain an integer (n) ((1 \leq n \leq 100)) representing the size of the matrix to be generated.

outputFormat

For each test case, output the generated (n \times n) matrix. Each row should contain (n) space-separated integers. Separate the matrices for different test cases by an empty line.## sample

1
2
1 2

4 3

</p>