#K44862. Snake Pattern Matrix
Snake Pattern Matrix
Snake Pattern Matrix
Given two positive integers \(n\) and \(m\), construct an \(n \times m\) matrix filled with distinct integers from 1 to \(n \times m\) arranged in a snake-like pattern. In this pattern, the rows of the matrix are filled alternately: the first row is filled from left to right, the second row from right to left, the third row from left to right, and so on.
Note: The snake pattern is defined as follows:
- For even-indexed rows (starting at 0), the row is filled from left to right.
- For odd-indexed rows, the row is filled from right to left.
Example: For \(n=3\) and \(m=4\), the resulting matrix is:
[ \begin{bmatrix} 1 & 2 & 3 & 4\ 8 & 7 & 6 & 5\ 9 & 10 & 11 & 12 \end{bmatrix} ]
inputFormat
The input consists of a single line containing two space-separated integers \(n\) and \(m\) representing the dimensions of the matrix.
\(1 \leq n, m \leq 100\)
outputFormat
Output the resulting snake pattern matrix. Each of the \(n\) lines should contain \(m\) integers separated by a single space. There should be no extra spaces at the end of each line.
## sample3 4
1 2 3 4
8 7 6 5
9 10 11 12
</p>