Snake-pattern matrix MEDIUM Expresii (BAC) Tablouri matrici (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Read two non-zero natural numbers `n` and `m` and print a matrix with `n` rows and `m` columns filled with consecutive natural numbers starting from `1`, in a snake pattern as shown in the example. On rows with an even index (the first, the third, ...) the numbers increase from left to right. On rows with an odd index (the second, the fourth, ...) the numbers increase from right to left, continuing the count.

Input format

Input input.txt

The program reads from standard input two natural numbers, `n` and `m`, separated by a space. - `1 <= n, m <= 50`

Output format

Output output.txt

The program prints to standard output the constructed matrix, with values on the same row separated by single spaces, one matrix row per output line.

Example

input
4 5
output
1 2 3 4 5

10 9 8 7 6

11 12 13 14 15

20 19 18 17 16

Example

input
4 5
output
1 2 3 4 5

10 9 8 7 6

11 12 13 14 15

20 19 18 17 16

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more