#C10396. Zigzag Matrix Generator

    ID: 39596 Type: Default 1000ms 256MiB

Zigzag Matrix Generator

Zigzag Matrix Generator

You are given multiple test cases. In each test case, you will be provided with two integers (M) and (N). Your task is to generate an (M \times N) matrix filled with integers from 1 to (M \times N) sequentially. The twist is that the matrix must be filled in a zigzag pattern:

  • In odd-indexed rows (1-indexed), fill the row from left to right.
  • In even-indexed rows (1-indexed), fill the row from right to left.

For example, for (M = 3) and (N = 3), the resulting matrix is:

[ \begin{bmatrix} 1 & 2 & 3\ 6 & 5 & 4\ 7 & 8 & 9 \end{bmatrix} ]

Make sure your program reads from standard input (stdin) and writes to standard output (stdout) exactly as specified.

inputFormat

The first line contains an integer (T), the number of test cases. Each of the following (T) lines contains two space-separated integers (M) and (N), representing the number of rows and columns of the matrix respectively.

outputFormat

For each test case, output the generated zigzag matrix. Each row of the matrix should be printed on a separate line with its elements separated by a single space. After each test case, print an empty line.## sample

1
3 3
1 2 3

6 5 4 7 8 9

</p>