eolymp
bolt
Try our new interface for solving problems
Məsələlər

Magic pyramid

Magic pyramid

There are many modifications of the famous Rubic’s Cube puzzle. One of those, called the Magic Pyramid, is based on a tetrahedron instead of the original cube. Faces of the tetrahedron are labeled with the Latin letters \textbf{X}, \textbf{U}, \textbf{V}, and \textbf{W}. Each face is divided into \textbf{9} equilateral triangles numbered from \textbf{1} to \textbf{36}, as shown on \textit{\textbf{Figure 1}}. To assemble the tetrahedron, the big triangle has to be folded down along the internal thick lines and the matching halves of the external thick lines have to be glued together. Each of the small triangles is painted into one of four distinct colors that we \includegraphics{https://static.e-olymp.com/content/43/43b7fa8f2b565d671c8f6507dfc01f1fa84611f4.jpg} \textit{\textbf{Fig. 1}} label with the Latin letters \textbf{a}, \textbf{b}, \textbf{c}, and \textbf{d}. There are exactly 9 triangles of each of the four colors. We can transform the tetrahedron using the operation of ”rotate a face”. To perform this operation, we choose a face of the pyramid and orient the pyramid so that the chosen face is towards us. Then we determine the direction of the rotation (clockwise or counter-clockwise). Finally, we turn the part of the tetrahedron closest to the chosen face and having the thickness of \textbf{1/3} of the height of the tetrahedron by \textbf{120} degrees in the chosen direction, leaving the remaining part of the tetrahedron unmoved. \textit{\textbf{Figure 2}} illustrates the effect of rotating the face \textbf{U} in clockwise direction. \includegraphics{https://static.e-olymp.com/content/2a/2a256a2983c086abab8c75397c1e9dc8f36ee46c.jpg} In addition to rotating the faces, we can also apply the operation of ”\textit{magic re-paint}”. This operation is defined by a permutation (\textbf{p_1}, …, \textbf{p_36}) of the integers \textbf{1} to \textbf{36}. The effect of the operation is that the triangle currently in position \textbf{i }(according to the numbering given on \textit{\textbf{Figure 1}}) is re-painted into the color of the triangle currently in position \textbf{p_\{i \}}(according to the same numbering). All \textbf{36} triangles are re-painted simultaneously. The puzzle is solved if, after applying a number of operations ”rotate a face” and/or ”magic re-paint” all triangles on each face of the tetrahedron have the same color. Write a program to solve the Magic Pyramid puzzle using the minimal possible number of moves. You may assume that all the test cases are solvable in \textbf{9} or less moves. \InputFile The first line of the file contains \textbf{36} letters '\textbf{a}'…'\textbf{d}'. The letter on the position \textbf{i} denotes the initial color of the triangle number \textbf{i}. It is known that each of the four letters occurs exactly \textbf{9} times on the first line of the file. The second line of the file contains \textbf{36} distinct integers \textbf{1…36} that describe the "magic re-paint" operation. The \textbf{i}^\{th \}integer on this line is the member \textbf{p_i} of the permutation that defines the operation. \OutputFile The first and only line of the file should contain a string of characters '\textbf{X}', '\textbf{U}', '\textbf{V}', '\textbf{W}', '\textbf{x}', '\textbf{u}', '\textbf{v}', '\textbf{w}', and '\textbf{*}'. The \textbf{i}^th character should describe the ith move in the solution of the puzzle. Each operation "rotate a face" is given as the label of the corresponding face, with lowercase letter denoting a turn in the clockwise and uppercase letter a turn in the counter-clockwise direction. The operation "magic re-paint" is given as the character '\textbf{*}'. If there are several solutions with the minimal number of moves, output any one of them. If the puzzle can be solved in no moves at all, output an empty line into the file. \includegraphics{https://static.e-olymp.com/content/82/82a67611578507b27da840643d201a4d5fbf4041.jpg} \textit{\textbf{Explanation of the example}}: \textit{\textbf{Figure 3}} shows the initial coloring of the tetrahedron, and \textit{\textbf{Figures 4 }}and \textit{\textbf{5}} show the results of each operation. The colors of the triangles are given by the following patterns: \includegraphics{https://static.e-olymp.com/content/cf/cf42918f67eb263cada1ed29e08157cd6e9d8e6b.jpg}
Zaman məhdudiyyəti 2 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
ddddcccccabbbbbdcaabbbddacccaabddaaa
36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Çıxış verilənləri #1
1
*U
Mənbə ACM Programming Contest 2005, Minsk, October 2005