#K67222. Rotate Square Matrix by 90° Clockwise
Rotate Square Matrix by 90° Clockwise
Rotate Square Matrix by 90° Clockwise
You are given an n × n square matrix. Your task is to rotate this matrix by \(90^\circ\) clockwise.
The rotation can be visualized as taking each element \(a_{i,j}\) from its original position and placing it in the new position \(a'_{j,n-1-i}\). This transformation ensures that the first row becomes the last column, the second row becomes the second-to-last column, and so on.
You need to handle multiple test cases. For each test case, the input starts with an integer indicating the dimension of the matrix followed by \(n\) lines each containing \(n\) integers separated by spaces. You should output the rotated matrix, where each row is printed on a new line with its elements separated by a space.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains a single integer \(T\), representing the number of test cases.
- For each test case:
- The first line contains a single integer \(n\), the size of the square matrix.
- The next \(n\) lines each contain \(n\) space-separated integers representing a row of the matrix.
outputFormat
For each test case, output the rotated matrix to standard output (stdout). The rotated matrix should consist of \(n\) lines where each line contains \(n\) space-separated integers. The matrices for different test cases are printed consecutively.
## sample1
3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>