#K74457. Diagonal Deflection of an Image Matrix
Diagonal Deflection of an Image Matrix
Diagonal Deflection of an Image Matrix
You are given an image represented by a matrix. The task is to deflect the image diagonally if and only if the matrix is square; that is, if the matrix has dimensions (n \times n). Deflection diagonally means reflecting the matrix over its main diagonal (i.e. transforming element (A_{ij}) to (A_{ji})). If the matrix is not square (i.e. the number of rows (n) is not equal to the number of columns (m)), output the string "Invalid Matrix".
For example, given a square matrix as input:
[ \begin{bmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 1 \end{bmatrix} ]
you should output its diagonal deflection (i.e. its transpose):
[ \begin{bmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 1 \end{bmatrix} ]
If the dimensions of the matrix are not equal, simply output "Invalid Matrix".
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. Each test case is described as follows:\
- The first line contains two space-separated integers (n) and (m) (number of rows and columns, respectively).\
- This is followed by (n) lines, each containing (m) space-separated integers, representing the matrix.
outputFormat
For each test case, if the matrix is square (i.e. (n = m)), print its diagonal deflection (transpose) as (n) lines with each line containing (n) space-separated integers. Otherwise, print the string "Invalid Matrix". Output for different test cases should be printed consecutively on separate lines.## sample
1
3 3
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
</p>