#P4536. Sierpinski Triangle Neighbors

    ID: 17782 Type: Default 1000ms 256MiB

Sierpinski Triangle Neighbors

Sierpinski Triangle Neighbors

Consider the construction of a Sierpinski triangle as follows. Start with an equilateral triangle (the parent triangle). Connect the mid‐points of its three sides to form 4 smaller triangles. Label these four triangles as \(T_1, T_2, T_3, T_4\) (see Figure 1). Then, recursively subdivide only the three corner triangles (those with labels ending in 1, 2, or 3) in the same manner. For example, subdividing \(T_1\) produces four triangles \(T_{11},T_{12},T_{13},T_{14}\) (see Figure 2). Continuing this process (always subdividing only those triangles whose label ends in 1,2, or 3) yields the Sierpinski triangle.

We say that a triangle \(A\) is adjacent to another triangle \(B\) if (i) \(B\) does not contain \(A\), and (ii) one of the complete edges of \(A\) is a part of one of the complete edges of \(B\). For example, \(T_{12}\) is adjacent to \(T_{14}\) and \(T_4\), but not adjacent to \(T_{32}\).

Given the label of one triangle in the Sierpinski triangle construction, determine all triangles (at the same subdivision level) that are adjacent to the given triangle. Print the labels of these neighboring triangles in lexicographical order, separated by a space.

inputFormat

The input is a single string, representing the label of a triangle in the Sierpinski triangle construction. The label always starts with a capital 'T' followed by one or more digits. For example: T12

outputFormat

Output the labels of all triangles that are adjacent to the given triangle (as defined above) at the same level of subdivision, in lexicographical order, separated by a single space.

sample

T12
T14 T4