#K53172. Matrix Rotation
Matrix Rotation
Matrix Rotation
You are given one or more square matrices. For each matrix, your task is to rotate the matrix 90° clockwise in place. The input contains multiple datasets. Each dataset begins with a positive integer \(n\) representing the size of the matrix (i.e., the number of rows and columns), followed by \(n\) lines with \(n\) space-separated integers representing the rows of the matrix. The input is terminated by a line containing -1
.
Your task is to output the rotated matrix for each dataset. After rotating, every matrix \(A\) should satisfy the relation:
[ A[i][j] \leftarrow A[n-1-j][i]\quad \text{for } 0 \leq i, j < n, ]
Separate the output for different matrices by an empty line.
inputFormat
The input is read from standard input and consists of multiple datasets. Each dataset is given as follows:
- The first line contains a single positive integer \(n\) (the dimension of the matrix) or
-1
to indicate the end of input. - The next \(n\) lines each contain \(n\) space-separated integers representing the rows of the matrix.
Input terminates with a line containing -1
.
outputFormat
For each matrix, output its rotated version (rotated 90° clockwise). Print each row on a new line with the numbers separated by a single space. Separate different matrices by an empty line.
## sample3
1 2 3
4 5 6
7 8 9
2
1 2
3 4
-1
7 4 1
8 5 2
9 6 3
3 1
4 2
</p>