#C6712. Spiral Matrix Traversal
Spiral Matrix Traversal
Spiral Matrix Traversal
Given a matrix of dimensions , your task is to output the spiral order traversal of the matrix. The spiral order starts from the top-left element and proceeds in a clockwise spiral until every element is visited.
The traversal order is: right, down, left, and up. This problem requires you to carefully handle various cases including matrices with a single row, a single column, or even an empty matrix.
inputFormat
The first line contains two space-separated integers and () representing the number of rows and columns respectively. The next lines each contain space-separated integers denoting the elements of the matrix.
outputFormat
Print a single line containing the elements of the matrix in spiral order as space-separated integers. If the matrix is empty (i.e. or ), output nothing.## sample
3 3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5