#C13005. Spiral Order Matrix Traversal

    ID: 42496 Type: Default 1000ms 256MiB

Spiral Order Matrix Traversal

Spiral Order Matrix Traversal

Given an ( m \times n ) matrix of integers, your task is to return the elements of the matrix in spiral order. The spiral order starts from the top-left element and moves rightwards, then proceeds downward, leftward, and upward in a decreasing boundary manner until all elements are traversed. This problem tests your ability to simulate multi-directional traversal over a 2D grid.

inputFormat

The first line of input contains two space-separated integers ( m ) and ( n ) representing the number of rows and columns of the matrix respectively. This is followed by ( m ) lines, each containing ( n ) space-separated integers representing the matrix elements.

outputFormat

Output a single line containing the elements of the matrix in spiral order, separated by a single space.## sample

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