#K53937. Rearrange Bookshelves
Rearrange Bookshelves
Rearrange Bookshelves
You are given one or more bookshelves. Each bookshelf is represented as a grid with M rows and N columns containing the heights of books. Your task is to rearrange the books on each shelf such that when the books are read in row-major order, they are in increasing order. In other words, if you flatten the grid into a single list and sort it, then place the sorted elements back into the grid row by row, both the rows and the overall order will be sorted.
For example, given a bookshelf with 2 rows and 3 columns:
3 2 1
6 5 4
After rearrangement, the bookshelf becomes:
1 2 3
4 5 6
The input consists of multiple test cases and ends with "0 0". Each test case should be processed separately and the rearranged bookshelf printed to standard output.
inputFormat
The input contains one or more test cases. Each test case begins with two integers M and N (1 ≤ M, N ≤ 100) separated by a space, indicating the number of rows and columns respectively. Following that, there are M lines with N integers each, representing the heights of the books. The input terminates with a line containing "0 0" which should not be processed.
outputFormat
For each test case, output the rearranged bookshelf in M lines, where each line contains N integers separated by a single space. The results for consecutive test cases should be printed one after another without extra blank lines.## sample
2 3
3 2 1
6 5 4
0 0
1 2 3
4 5 6
</p>