#P6347. Harmonious Array Shift Puzzle

    ID: 19563 Type: Default 1000ms 256MiB

Harmonious Array Shift Puzzle

Harmonious Array Shift Puzzle

You are given an n × m 2D array with all entries distinct and numbered from 0 to n*m - 1. The final "harmonious" (solved) array is defined as the one in which the first row is [0, 1, 2, …, m-1], the second row is [m, m+1, …, 2*m-1], and so on.

You are allowed only two types of cyclic shift operations on the array:

  • A right shift on a row: in one move you choose a row, and cyclically shift all its elements to the right by any number of positions.
  • A down shift on a column: in one move you choose a column, and cyclically shift all its elements downward by any number of positions.

For example, consider the 4×4 array below:

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

If you shift the second column (0-indexed) down by 1 (which is equivalent to shifting it up by 3), the array becomes:

0  13 2  3
4  1  6  7
8  5  10 11
12 9  14 15

Note that a left shift by k is equivalent to a right shift by m − k (and similarly, upward shift is equivalent to a down shift). Hence, we restrict the allowed operations to right shifts and down shifts.

Your task is to find a series of such operations that transforms the given array into the harmonious array. You should output a valid sequence of operations. The format for an operation is as follows:

  • For a row right shift: output a line with R i k, meaning row i is shifted right by k positions.
  • For a column down shift: output a line with C j k, meaning column j is shifted down by k positions.

The first line of your output must be the total number of operations. If no operations are needed, simply output 0.

Note: The rows and columns are 0-indexed.

inputFormat

The first line contains two integers n and m — the dimensions of the array.

The next n lines each contain m integers, representing the rows of the array.

outputFormat

First, output an integer L denoting the number of operations performed.

Then, output L lines, each describing an operation in one of the following two formats:

  • R i k — row i is cyclically shifted right by k positions.
  • C j k — column j is cyclically shifted down by k positions.

The resulting array after applying all operations must be the harmonious array.

sample

4 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
0

</p>