Trapezoid puzzle
Time limit0.5sMemory limit1024 MB
Tile a triangular-grid hexagon of shaded cells with 3-triangle trapezoids, backtracking in a fixed canonical order and colouring pieces greedily so no two equal colours share an edge.
- Level
Medium6 of 10
- Topics
- Backtracking, Greedy, Simulation, Geometry
- Solved
- No attempts yet
Problem
Draw equidistant parallel lines between each of the three pairs of opposite sides of a regular hexagon. The lines divide the hexagon into equilateral triangles, and the result is a hexagonal puzzle of size . Some of the triangles in the puzzle are shaded and need to be covered with puzzle pieces. Each piece is a trapezoid made of three equilateral triangles placed side by side. The pieces come in 6 colours, numbered 1 to 6, and an unlimited number of pieces of each colour is available.

Figure 1: The puzzle of size 3 from the first sample and one way to cover it.
The pieces must be placed on the hexagon so that all of the following hold:
- Each piece fully covers three shaded triangles.
- Each shaded triangle is covered by exactly one piece.
- Two pieces of the same colour do not touch along the side of a triangle (they may touch at a corner).
Determine whether the puzzle can be solved, and if it can, find the solution defined in the output section.
Input
The first line contains a positive integer , the size of the puzzle ().
The next lines describe the rows of the puzzle from top to bottom. Each line is a string that describes the triangles of one row from left to right. The digit 0 is a shaded triangle and . (dot) is a triangle that is not shaded. From the top, the rows have lengths . Along a row, the triangles alternate in direction. In the upper rows the first triangle points up, and in the lower rows the first triangle points down.
At least one triangle is shaded.
Output
If the puzzle cannot be solved, print nemoguce (Croatian for "impossible") on the first line.
Otherwise, print lines that describe the solution in the same format as the input. Replace the 0 of every shaded triangle with the colour of the piece that covers it, a digit from 1 to 6, and keep every . as it is.
The placement and the colours must follow these rules, so that the answer is unique. The reading order of the triangles goes through the rows from top to bottom, and through each row from left to right.
- Scan the shaded triangles in reading order. Whenever the scan reaches a shaded triangle that is not covered yet, place a new piece whose earliest triangle in reading order is this triangle. Only pieces that still allow all remaining shaded triangles to be covered are candidates. Among the candidates, choose the one whose second triangle in reading order comes earliest, and if that is tied, the one whose third triangle comes earliest.
- Colour the pieces in the order they were placed. Give each piece the smallest colour number that is not used by any earlier piece sharing a triangle side with it. A piece touches at most 5 other pieces along sides, so such a colour always exists.
The covering in Figure 1 is only one valid covering and can differ from the answer these rules define.