#K53242. Spiral Order Matrix Traversal
Spiral Order Matrix Traversal
Spiral Order Matrix Traversal
In this problem, you are given a 2D matrix of dimensions . Your task is to traverse the matrix in spiral order and output the elements as they are visited. The spiral traversal starts from the top-left corner and proceeds in a clockwise manner. Specifically, you first traverse right, then down, then left, and finally up and repeat this process until every element has been visited.
Input constraints:
- The first line contains two integers, (number of rows) and (number of columns).
- The next lines each contain integers, the elements of the matrix.
Output:
- Print the matrix elements in spiral order separated by a single space.
inputFormat
The input is read from standard input (stdin). It begins with two integers and where is the number of rows and is the number of columns of the matrix. This is followed by lines, each containing space-separated integers representing the matrix.
outputFormat
Output the elements of the matrix in spiral order on a single line, separated by a 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