Aztec Diamond
Time limit1sMemory limit128 MB
Given a domino tiling of an Aztec diamond, find the shortest sequence of 2x2 rotations that turns all bricks vertical, lexicographically smallest.
- Level
Hard8 of 10
- Topics
- Greedy, Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
Jaemin found an old diamond shaped pattern paved with bricks and no gaps.

A pattern of size sits inside a grid with rows and columns. For the -th row from the top, let . That row holds cells, running from the -th column to the -th column from the left. Every cell of the pattern is covered by exactly one brick.
Jaemin dislikes the irregular mix of horizontal and vertical bricks, so he wants to make every brick vertical. His hands are small, so he cannot lift a brick with one hand, but he can pinch two bricks between his hands and lift them. The only operation he can perform is to pick a square that exactly two bricks cover and rotate it by degrees.

One rotation turns two horizontal bricks stacked one above the other into two vertical bricks placed next to each other, or the other way round.
A programming contest starts soon, so Jaemin does not want to spend much time on this. Help him.
Input
The first line holds the size of the pattern. is between and .
Each of the next lines holds a string of length that describes one row of the pattern. . is a cell outside the pattern, L and R are the left and right cells of a horizontal brick, and U and D are the top and bottom cells of a vertical brick.
Output
On the first line, print the minimum number of rotations needed to make every brick vertical. On each of the next lines, print the row number and the column number of the top left cell of the square to rotate, in the order the rotations happen. The top row and the leftmost column are numbered .
If more than one sequence of rotations works, compare the sequences as the number lists and print only the one that comes first in lexicographic order. Every input that satisfies the constraints has an answer.