#C8497. Zigzag Matrix Traversal

    ID: 52485 Type: Default 1000ms 256MiB

Zigzag Matrix Traversal

Zigzag Matrix Traversal

Given a 2D matrix of integers with m rows and n columns, traverse it in a zigzag order. In other words, for each row with an even index (starting from 0), traverse from left to right; for rows with an odd index, traverse from right to left. The output should be the list of numbers in the order they are visited.

In mathematical terms, if we denote the matrix as \(A_{ij}\), the output order is:

\(A_{0,0}, A_{0,1}, \ldots, A_{0,n-1}, A_{1,n-1}, A_{1,n-2}, \ldots, A_{1,0}, \ldots, A_{m-1,?}\)

If the matrix is empty, output an empty line.

inputFormat

The first line contains two integers m and n, representing the number of rows and columns in the matrix. Each of the next m lines contains n integers separated by spaces.

outputFormat

Output a single line containing the elements of the matrix in zigzag order, with each element separated by a single space.

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