Aztec Diamond

Time limit1sMemory limit128 MB

Summary
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 1×21 \times 2 bricks and no gaps.

A pattern of size NN sits inside a grid with 2N2N rows and 2N2N columns. For the ii-th row from the top, let k=min⁡(i,2N+1−i)k = \min(i, 2N+1-i). That row holds 2k2k cells, running from the (N−k+1)(N-k+1)-th column to the (N+k)(N+k)-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 2×22 \times 2 square that exactly two bricks cover and rotate it by 9090 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 NN of the pattern. NN is between 11 and 100100.

Each of the next 2N2N lines holds a string of length 2N2N 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 DD needed to make every brick vertical. On each of the next DD lines, print the row number and the column number of the top left cell of the 2×22 \times 2 square to rotate, in the order the rotations happen. The top row and the leftmost column are numbered 11.

If more than one sequence of DD rotations works, compare the sequences as the number lists r1,c1,r2,c2,…,rD,cDr_1, c_1, r_2, c_2, \ldots, r_D, c_D and print only the one that comes first in lexicographic order. Every input that satisfies the constraints has an answer.

Examples2

  1. Example 1

    Input
    3
    ..UU..
    .UDDU.
    UDLRDU
    DUULRD
    .DDLR.
    ..LR..
    
    Expected output
    4
    4 4
    4 3
    3 3
    5 3
    
  2. Example 2

    Input
    2
    .UU.
    UDDU
    DUUD
    .DD.
    
    Expected output
    0