eolymp
bolt
Try our new interface for solving problems
Problems

Drift Puzzle

Drift Puzzle

A common puzzle involves listing a sequence of three frames (\textbf{F_1}, \textbf{F_2}, \textbf{F_3}) of an animation, and asking for the "most logical" fourth frame \textbf{F_4}. For example: \includegraphics{https://static.e-olymp.com/content/ed/edbe7202a52e9eb68bd5b480e873b6e801c43322.jpg} Usually the puzzle is posed as multiple-choice question. This is boring. A more interesting problem is to find the fourth frame directly! To be precise, we will consider animation frames that are constructed using the following rules: \begin{itemize} \item Every frame \textbf{F_i} is a grid with height \textbf{h} and width \textbf{w}. \item Every frame \textbf{F_i} contains \textbf{n} distinct objects \textbf{m_1 = A}, \textbf{m_2 = B}, \textbf{m_3 = C}, ..., \textbf{m_n = (letter)}. \item Each object \textbf{m_j} in each frame is located in one of \textbf{h}×\textbf{w} cells, and may overlap. \item If two objects \textbf{m_j} and \textbf{m_k}, \textbf{j} < \textbf{k} occupy the same cell, then only \textbf{m_j} (i.e. the object with lesser index) would be visible. \item Each object \textbf{m_j} has a fixed, non-zero "drift velocity": From \textbf{F_i} to \textbf{F_i_\{+1\}}, \textbf{m_j} travels \textbf{1} unit in one of eight possible (horizontal, vertical or diagonal) directions. \item If an object moves outside the grid, it "wraps around" to the opposite side (or corner). \end{itemize} In the example shown, we can deduce that \textbf{A} drifts eastward; \textbf{B} drifts south-westward; \textbf{C} drifts northward; \textbf{D} drifts northward; and \textbf{E} drifts north-westward. In \textbf{F_2}, \textbf{B} and \textbf{C} occupy the same cell, so only \textbf{B} is visible. In \textbf{F_3}, \textbf{B} drifts beyond the west wall, and wraps around on the east side (while also moving south by one unit). Using the above information, we can extrapolate the trajectory of each object, and find \textbf{F_4}: \includegraphics{https://static.e-olymp.com/content/30/308a7880cad853aec67590f93c2b571c60c03181.jpg} Since objects cover one another, \textbf{F_1}, \textbf{F_2} and \textbf{F_3} may not provide enough information to solve for the velocities of each object. Therefore \textbf{F_4} can have multiple solutions (see Sample Input case \textbf{2}); when this happens you should output an error message. Sometimes objects can have many possible velocities, but a unique solution for \textbf{F_4} would still exist (see Sample Input case \textbf{3}); in this case \textbf{F_4} is well-defined, and should be solved. \InputFile The first input line contains the number of test cases \textbf{N}, \textbf{1} ≤ \textbf{N} ≤ \textbf{50}. Each test case begins with a line containing integers \textbf{h}, \textbf{w} and \textbf{n}, separated by space. \textbf{h} lines follow. On each line \textbf{i}, \textbf{1} ≤ \textbf{i} ≤ \textbf{h}, three space-separated length-\textbf{w} strings \textbf{a_i}, \textbf{b_i} and \textbf{c_i} are given. The text block \textbf{a_1}, \textbf{a_2}, ..., \textbf{a_h}, when stacked vertically, specifies \textbf{F_1}. Similarly, \textbf{b_1}, \textbf{b_2}, ..., \textbf{b_h} and \textbf{c_1}, \textbf{c_2}, ..., \textbf{c_h} specify the \textbf{F_2} and \textbf{F_3}, respectively. \begin{itemize} \item \textbf{h} is the height of each frame, and satisfies \textbf{3} ≤ \textbf{h} ≤ \textbf{10}. \item \textbf{w} is the width of each frame, and satisfies \textbf{3} ≤ \textbf{w} ≤ \textbf{10}. \item \textbf{n} is the number of distinct masses, and satisfies \textbf{1} ≤ \textbf{n} ≤ \textbf{26}. \item For \textbf{1} ≤ \textbf{i} < \textbf{h}, each \textbf{a_i}, \textbf{b_i} and \textbf{c_i} are length-\textbf{w} strings consists of upper case alphabets "\textbf{A}"-"\textbf{Z}" (for objects) or the period "\textbf{.}" (for empty spaces). \item \textbf{F_1}, \textbf{F_2} and \textbf{F_3} from inputs will follow animation construction rules outline above, so \textbf{F_4} is guaranteed to have at least one solution. \end{itemize} \OutputFile For each test case, if a unique \textbf{F_4} exists, then print it in the same format as the input, i.e. as \textbf{h} lines of length-\textbf{w} strings. Otherwise print "\textbf{Multiple solutions exist}" on a single line. Print an empty line between test cases, but not after the last test case. For Test Case \textbf{2}, two possible solutions for \textbf{F_4} are \textbf{ABC ABC} \textbf{DE. DEF} depending whether \textbf{F} drifts northward or drifts westward; so multiple solutions exist. For Test Case \textbf{3}, \textbf{G} may drift eastward, southward or south-eastword, but all three cases would lead to the same \textbf{F_4}, so a unique solution exists. For Test Case \textbf{4}, \textbf{B-D} are covered by \textbf{A}, and \textbf{F}-\textbf{Z} are covered by \textbf{A} or \textbf{E}. Therefore the location of some blocks are ambiguous. However, a unique solution exists. For Test Case \textbf{5}, Two possible \textbf{F_4}'s are \textbf{..... .....} \textbf{..C.. ..C..} \textbf{..... .....} \textbf{A.... A..D.} \textbf{.B... .B...}
Time limit 1 second
Memory limit 64 MiB
Input example #1
5
4 4 5
A... .A.. .DA.
.B.. .DE. C...
.D.E B... ....
C... .... ...B
2 3 6
ABC CAB BCA
DEF .DE E.D
3 3 7
GAB AB. B.A
CE. DF. ...
D.F ... C.E
4 4 26
A.E. .... ....
.... A.E. ....
.... .... A.E.
.... .... ....
5 5 4
A.... ..... .B...
..... AB... .....
.B... ..... A.C..
..... ..C.. .....
..C.. ..... .....
Output example #1
C.BA
....
....
ED..

Multiple solutions exist

GAB
CE.
D.F

....
....
....
A.E.

Multiple solutions exist
Source University of Toronto 2010 ACM Tryout: Saturday, October 2, 2010