#C12652. Matrix Rotation

    ID: 42103 Type: Default 1000ms 256MiB

Matrix Rotation

Matrix Rotation

You are given a two-dimensional matrix with \( r \) rows and \( c \) columns. Your task is to rotate the matrix 90° clockwise. Before performing the rotation, validate that the matrix is non-empty and that every row contains the same number of elements.

For example, given the matrix:

1 2
3 4

Its 90° clockwise rotated version is:

3 1
4 2

If the matrix is not valid (i.e. empty or with rows of unequal lengths), you should throw an error. In this problem, you need to implement the matrix rotation and output the rotated matrix.

inputFormat

Input is read from stdin in the following format:

  • The first line contains two integers \( r \) and \( c \), representing the number of rows and columns of the matrix.
  • The following \( r \) lines each contain \( c \) space-separated integers denoting the elements of the matrix.

outputFormat

Output the rotated matrix to stdout in the following format:

  • Each line represents a row of the rotated matrix with its elements separated by a single space.
## sample
2 2
1 2
3 4
3 1

4 2

</p>