Problem 494
Reshape the Matrix
Easy · 10 pts · Matrix, Simulation
Problem
Reshape an R x C matrix into a new matrix with r rows and c columns, keeping the elements in the same row-by-row reading order. This is only possible when R · C = r · c. If the reshape is not possible, print the original matrix unchanged. Reading the original row by row and writing the new one row by row does the job.
Input
First line: R, C, r and c. Next R lines: C integers each.
Output
Print the reshaped matrix (or the original one if the reshape is impossible).
Constraints
1 <= R, C, r, c <= 100; -1000 <= value <= 1000
Example input
2 2 1 4 1 2 3 4
Example output
1 2 3 4
Explanation
The 2 x 2 matrix 1 2 / 3 4 read row by row is 1 2 3 4, which fits a 1 x 4 matrix as the single row 1 2 3 4.