#C1165. Matrix Rotation
Matrix Rotation
Matrix Rotation
You are given a matrix. The first line of the input contains two integers m and n which represent the number of rows and columns respectively. The next m lines each contain n space-separated integers. Your task is to rotate the matrix by \(90^\circ\) clockwise in-place if and only if the matrix is non-empty and square (i.e. \(m=n\)). Otherwise, output None
.
Examples:
- For a 3x3 matrix:
Input:3 3
1 2 3
4 5 6
7 8 9
Output:7 4 1
8 5 2
9 6 3 - For a non-square 2x3 matrix, output is
None
.
Make sure to handle edge cases such as an empty matrix (indicated by "0 0" as dimensions) or a 1x1 matrix.
inputFormat
The input consists of multiple lines. The first line contains two space separated integers \(m\) and \(n\) representing the dimensions of the matrix. If either \(m\) or \(n\) is zero, the matrix is considered empty. The following \(m\) lines each contain \(n\) integers separated by spaces.
outputFormat
If the matrix is square and non-empty, output the rotated matrix in the form of \(m\) lines, where each line contains \(m\) space-separated integers. If the matrix is not square or is empty, output None
(without quotes).
3 3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>