#P4578. Unlocking Solomon's Treasure

    ID: 17824 Type: Default 1000ms 256MiB

Unlocking Solomon's Treasure

Unlocking Solomon's Treasure

According to ancient legends, King Solomon was not only a symbol of wisdom but also of immense wealth. His mysterious treasure – a hoard of gold, ivory, and diamonds – was hidden away behind an impenetrable door. The door is unlocked by a peculiar rectangular numeric keypad. At the left of each row and at the top of each column there is a ruby button. Each button can be rotated left or right. Rotating a button to the left increases every number in the corresponding row (or column) by 1, while rotating it to the right decreases them by 1.

In several specific cells of the matrix, green gems are embedded. Only when each green gem cell displays the secret number recorded on the treasure map will the door slowly open. Originally, in order to thwart treasure hunters, the witch set the initial state of every cell in the array to 0. Under pressure, the heroes must quickly find a sequence of moves (rotations on the ruby buttons) that changes the numbers at the green gem positions to match exactly the password given by the treasure map.

Formally, assume the keypad is an R×C matrix (initially all zeros). Let x[i] (1 ≤ i ≤ R) be the cumulative change caused by operations on the i-th row's button, and y[j] (1 ≤ j ≤ C) be that from the j-th column's button. A left rotation adds 1 and a right rotation subtracts 1. Thus, after all moves the cell (i, j) will have value x[i] + y[j]. For each green gem cell at position (i, j) with target number A, we require:

\(x_i + y_j = A\)

Find any sequence of moves (each move is a rotation on a particular row button or column button) such that the cells containing green gems show the correct password numbers. You may assume that a solution exists. The number of moves is not required to be minimal.

inputFormat

The first line contains three integers R, C, and K, representing the number of rows, columns, and the number of green gem positions, respectively.

Each of the following K lines contains three space-separated integers: i, j, and A, which mean that the cell located at row i and column j (1-indexed) must have the value A after all operations.

You may assume that 1 ≤ R, C ≤ 1000, and 1 ≤ K ≤ R×C.

outputFormat

The output should start with an integer M on the first line – the total number of moves. Each of the following M lines should describe one move in the order they are performed. Each move is described in the format:

row i left

or

row i right

or for a column button:

col j left

or

col j right

where i (1 ≤ i ≤ R) is a row number and j (1 ≤ j ≤ C) is a column number. Rotating left adds 1 to every number in that row or column, and rotating right subtracts 1. The cumulative effect of these operations on any cell (i, j) will be x[i] + y[j].

sample

2 2 2
1 1 3
2 2 1
4

row 2 left col 1 left col 1 left col 1 left

</p>