cswork / OAOpen practice workspace

MetaOA #14

Diagonal Traverse (for E4 ;)

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationIn-place Arrays

Starting from the top-left corner, traverse the diagonals in increasing order of the row-plus-column index sum. Even index sums go from bottom-left to top-right, and odd index sums go from top-right to bottom-left. Output the traversal sequence. The order in the original site's 3×3 example is wrong, so this site corrects it according to the standard alternating rule.

Input

This site's input scale: R C (1..100), followed by R lines of C integers each (−10⁹..10⁹).

Output

Output the answer as described; separate multiple results with spaces in order, and output an empty line for an empty array.

Example 1

Input:

3 3
1 2 3
4 5 6
7 8 9

Output:

1 2 4 7 5 3 6 8 9

Example 2

Input:

2 2
1 2
3 4

Output:

1 2 3 4

Example 3

Input:

1 1
5

Output:

5

Example notes

Example 1: the diagonals output [1], [2,4], [7,5,3], [6,8], [9] in order, and concatenating them gives the result.

Editorial

Loading solution…