eolymp
bolt
Try our new interface for solving problems
Problems

Domino Tiling

Domino Tiling

In this problem, you have to transform one domino tiling of a rectangle into another by moving dominoes along cycles. Consider a rectangular grid of size \textbf{h} × \textbf{w}. A domino piece (or simply domino) is a rectangular block that can be placed on any two cells of the grid sharing a common side. A tiling of the grid is a collection of domino pieces such that each cell of the grid is covered by exactly one piece in the collection. Note that dominoes may not cross the border of the grid. You are given two tilings of the grid: initial tiling and target tiling. Your task is to transform initial tiling into target tiling. In order to achieve that, you are allowed to make moves. A single move consists in picking a domino cycle and moving dominoes along that cycle (the formal definition is given below). The cost of each move is the number of cells in the respective domino cycle. The total cost of transformation is the sum of costs of all moves you make. Of course, the total cost must be minimal possible. Now, to the details. Let us define a domino cycle of positive length \textbf{n} on a given tiling. Consider a circular sequence of \textbf{2n} distinct cells such that any two consecutive cells in the sequence share a common side. Here, circular means that the first and last cells of the sequence are also considered consecutive. Obviously, there are \textbf{2n} pairs of consecutive cells in such a sequence. Cover \textbf{n} of them by \textbf{n} non-overlapping domino pieces and you get a domino cycle. The process of moving dominoes along a cycle results in change of the tiling. First, the \textbf{n} domino pieces covering all the \textbf{2n} cells which belong to the cycle are removed. After that, \textbf{n} new non-overlapping domino pieces are added to cover \textbf{n} other pairs of consecutive cells. One can easily see that the resulting collection of pieces is also a tiling. Recall that the cost of such move is \textbf{2n}. \InputFile The first line contains two integers \textbf{h} and \textbf{w} separated by a space: height and width of the rectangular grid (\textbf{1} ≤ \textbf{h}, \textbf{w} ≤ \textbf{100}). The next \textbf{h} lines contain \textbf{w} characters each and describe initial tiling. Each of these characters is one of the following: \begin{itemize} \item character “\textbf{l}” denotes a left part of a domino piece, \item character “\textbf{r}” denotes a right part of a piece, \item character “\textbf{u}” denotes an upper part, and \item character “\textbf{d}” corresponds to a lower part. \end{itemize} Next goes a line containing \textbf{w} dash signs (“-”). The next \textbf{h} lines contain \textbf{w} characters each and describe target tiling in the same fashion. It is guaranteed that the input is consistent: \begin{itemize} \item \textbf{h} and \textbf{w} are chosen in such a way that tiling an \textbf{h}× \textbf{w} grid is possible \item each character in each tiling is guaranteed to have the required neighbor in the required direction. \end{itemize} \OutputFile On the first line write two integers separated by a space: \textbf{m}, the number of moves, and \textbf{p}, the total cost of transformation. Note that as long as the cost p is minimal possible, the number of moves m can be arbitrary. The next \textbf{m} lines should describe the moves, one per line. A move along a domino cycle of \textbf{k} cells should be written as \textbf{k r_1 c_1 r_2 c_2 ... r_k c_k}. Here, \textbf{r_i} and \textbf{c_i} are the coordinates of \textbf{i}-th cell of the cycle. Separate consecutive tokens on a line by one or more spaces. You can start a cycle at any point and traverse it in any direction. \Note In the first example, the minimal total cost is \textbf{10}. It can be achieved by just one move which involves the \textbf{10} border cells of the grid in cyclic order. Five dominoes covering cells (\textbf{1}, \textbf{1}) and (\textbf{1}, \textbf{2}), (\textbf{1}, \textbf{3}) and (\textbf{1}, \textbf{4}), (\textbf{2}, \textbf{4}) and (\textbf{3}, \textbf{4}), (\textbf{3}, \textbf{3}) and (\textbf{3}, \textbf{2}), (\textbf{3}, \textbf{1}) and (\textbf{2}, \textbf{1}) are removed. After that, new dominoes covering cells (\textbf{1}, \textbf{2}) and (\textbf{1}, \textbf{3}), (\textbf{1}, \textbf{4}) and (\textbf{2}, \textbf{4}), (\textbf{3}, \textbf{4}) and (\textbf{3}, \textbf{3}), (\textbf{3}, \textbf{2}) and (\textbf{3}, \textbf{1}), (\textbf{2}, \textbf{1}) and (\textbf{1}, \textbf{1}) are added. The answer is illustrated below on the left. Note that there could be other ways of achieving the target tiling. To the right is another solution which is however not optimal. \includegraphics{https://static.e-olymp.com/content/3a/3ab4eadb68e89491affb8edde234e3670b0ea899.jpg} In the second example, the minimal total cost is \textbf{12}. It can be achieved by moving the dominoes along three small cycles each of which contains four cells. The process is illustrated below. \includegraphics{https://static.e-olymp.com/content/20/2086f525eb4b81e29e954c7d6c8362f96f41f4e9.jpg}
Time limit 1 second
Memory limit 64 MiB
Input example #1
3 4
lrlr
ulru
dlrd
----
ulru
dlrd
lrlr
Output example #1
1 10
10  1 1  1 2  1 3  1 4  2 4  3 4  3 3  3 2  3 1  2 1
Source 2014 Petrozavodsk, Ivan Kazmenko Contest, August 22, Problem D