eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

The Alliances

The Alliances

In a fantasy world, there is a large island of a rectangular shape. The sides of the island happen to be exactly \textbf{R}miles and \textbf{C} miles long, and the whole island is divided into a grid of \textbf{R}×\textbf{C} areas. Some of the areas are uninhabited, and the rest are occupied by villages of fantasy beings: elves, humans, dwarves, and hobbits. Each area contains at most one village. Two villages are considered neighbours if their areas share a side. Recently, the villages became terrified of the Great Evil. In order to feel safer, each village has decided to form military alliances with some of its neighbours. An alliance always involves two neighbouring villages, and it is a mutual and symmetric agreement. Depending on the species living in the village, the inhabitants will not feel safe unless a specific configuration of alliances is formed: \begin{itemize} \item The elves feel confident, and therefore need exactly one alliance. \item Human villages require alliances with exactly two neighbours. Moreover, leaving two opposite sides of the village exposed is bad for tactical reasons. Therefore the two allied neighbours cannot be located on opposite sides of the village. \item Dwarves require alliances with exactly three neighbours. \item Hobbits are extremely scared, and therefore need to have alliances with all four of their neighbours. \end{itemize} In other words, except for humans each village requires a particular number of alliances, but does not care which neighbours will be its allies. For humans there is an additional restriction: the allied neighbours must not be on opposite sides of the village. The conditions must be fulfilled irrespective of the position of the village on the map. For example, a dwarf village desires three alliances. If it is located on the coast, this means that it must have alliances with all three neighbours. If there is a dwarf village in a corner of the island, its inhabitants will never feel safe. For a given island description, your task is to decide whether it is possible to form alliances so that all inhabitants of the island will feel safe. In case of a positive answer, your task is also to suggest one viable configuration of alliances. In case of multiple solutions, choose an arbitrary one. \InputFile The first line of the input contains two integers \textbf{R} and \textbf{C} specifying the size of the island. The following \textbf{R} lines encode a description of the island. Each line consists of \textbf{C} space-separated numbers between \textbf{0} and \textbf{4}: \begin{itemize} \item \textbf{0} means uninhabited area, \item \textbf{1} means an elf village, \item \textbf{2} means a human village, \item \textbf{3} means a dwarf village, \item \textbf{4} means a hobbit village. \end{itemize} (Note that the number in the input always corresponds to the number of desired alliances for that village.) \textbf{Constraints} In all test cases assume that \textbf{1} ≤ \textbf{R}, \textbf{C} ≤ \textbf{70}. In test cases worth a total of \textbf{55} points we have \textbf{min(R, C)} ≤ \textbf{10}. Out of these, in test cases worth \textbf{15} points \textbf{R·C} ≤\textbf{20}. Another batch of test cases worth \textbf{10} points contains maps with only uninhabited areas and human villages. (This batch is not included in the test cases worth \textbf{55} points.) \OutputFile If there is no solution, output a single line with the string "\textbf{Impossible!}" (quotes for clarity). Otherwise, output one valid map of alliances in the following format. Each area should appear in the output as a matrix of \textbf{3}×\textbf{3} characters. If the area is uninhabited, the corresponding section of the output will be filled with \textbf{.} (dot) symbols. If the area has a village there should be a a symbol \textbf{O}(uppercase letter \textbf{O}) in the middle representing the village itself, and there should be symbols \textbf{X} (uppercase letter \textbf{X}) representing a configuration of its allies. The rest of the matrix should be filled with \textbf{.} (dot) symbols. For each village type, all possible layouts of alliances are shown in the image below. \includegraphics{https://static.e-olymp.com/content/8e/8e12c81c158590e3c16470096dffe579bf69845e.jpg}
Лимит времени 2 секунды
Лимит использования памяти 256 MiB
Входные данные #1
3 4
2 3 2 0
3 4 3 0
2 3 3 1
Выходные данные #1
............
.OXXOXXO....
.X..X..X....
.X..X..X....
.OXXOXXO....
.X..X..X....
.X..X..X....
.OXXOXXOXXO.
............
Автор Marek Zeman