#K56902. Spiral Matrix Traversal
Spiral Matrix Traversal
Spiral Matrix Traversal
Given a 2D matrix, your task is to traverse the matrix in spiral order and print the resulting sequence. In spiral order traversal, you first traverse the top row from left to right, then the right column from top to bottom, followed by the bottom row from right to left, and finally the left column from bottom to top. This process is repeated for the remaining submatrix until all elements have been traversed.
Specifically, if the matrix is represented as $$A_{ij}$$ where $$i$$ denotes the row index and $$j$$ the column index, you need to output the sequence of (A_{ij}) in spiral order.
inputFormat
The input is read from standard input (stdin).
Format:
On the first line, two integers (m) and (n) represent the number of rows and columns respectively. Then follow (m) lines, each containing (n) space-separated integers which represent the elements of the matrix.
outputFormat
Print the elements of the matrix in spiral order in a single line, separated by a single space. The output is written to standard output (stdout).## sample
3 3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5