#K47597. Matrix Rotation
Matrix Rotation
Matrix Rotation
You are given a 2D matrix containing integers. Your task is to rotate the matrix by 90° clockwise.
The rotation moves the elements in such a way that the first row becomes the last column, the second row becomes the second-to-last column, and so on.
Note: The matrix can be of any dimensions (including non-square matrices). If the matrix is empty (0 rows or 0 columns), output an empty result.
The rotated matrix will have its dimensions swapped: if the input matrix has n rows and m columns, the rotated matrix will have m rows and n columns.
The rotation operation can be understood mathematically as follows:
\( \text{rotated}[i][j] = \text{original}[n-1-j][i] \)
inputFormat
The first line of input contains two integers n and m, denoting the number of rows and columns, respectively. The following n lines each contain m integers separated by spaces, forming the matrix.
If n = 0 or m = 0, then there is no matrix data and the output should be empty.
outputFormat
Output the rotated matrix. Each line should represent a row of the rotated matrix with the integers separated by a single space.
## sample3 3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>