#C764. Spiral Order Matrix Traversal
Spiral Order Matrix Traversal
Spiral Order Matrix Traversal
Given an \(N \times M\) integer matrix, your task is to print the elements of the matrix in spiral order. The spiral order is obtained by traversing the outer layer of the matrix first and then proceeding inwards layer by layer.
For example, consider the following \(3 \times 3\) matrix:
1 2 3 4 5 6 7 8 9
The spiral order for this matrix is: 1 2 3 6 9 8 7 4 5.
Note: The matrix dimensions and elements are provided via standard input, and the output should be printed on a single line with values separated by spaces.
inputFormat
The first line contains two integers \(N\) and \(M\), representing the number of rows and columns of the matrix, respectively.
This is followed by \(N\) lines, each containing \(M\) integers separated by spaces, representing the rows of the matrix.
outputFormat
Output a single line containing the elements of the matrix in spiral order, separated by a single space.
## sample3 3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5