Skills101
← All coding problems

Snake Pattern Traversal

easyMatrix+10 XPTime limit 2s

Print the matrix in snake order: the first row from left to right, the second row from right to left, the third from left to right again, and so on — like a snake sliding down a board.

Input:

  • line 1: r c
  • next r lines: c integers each

Output: all r·c values on one line, in snake order.

Constraints: 1 ≤ r, c ≤ 500, −10⁹ ≤ value ≤ 10⁹.

Think about it: even-numbered rows (counting from 0) go left to right; odd-numbered rows go right to left.

Sample input 1

3 3
1 2 3
4 5 6
7 8 9

Sample output 1

1 2 3 6 5 4 7 8 9

Sample input 2

2 4
1 2 3 4
5 6 7 8

Sample output 2

1 2 3 4 8 7 6 5

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.