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

Coalescing Continents

Coalescing Continents

\includegraphics{https://static.e-olymp.com/content/1a/1a07975533e60ddce2cd3e0f0a0d783608718494.jpg} Continental drift refers to the idea that once the earth’s continents were all in one piece and with time they drifted away from each other and arrived at their present positions. In this problem, we are concerned with the reverse process -- \textit{Continental Coalesce}. For the sake of brevity, let’s assume the continents were initially merged together forming a square. With time, the square disintegrated into \textbf{K} different continents in the shape of rectangles and drifted away from its source. You are provided with the current locations of the continents (i.e. rectangles). These locations were outlined with the help of satellites. Your first job is to figure out whether the data provided is a valid one. The data is valid if you can move the rectangles and form a square. In one move, you can pick any rectangle and push it one unit in one of the four directions (north, south, east or west). The continents can slide underneath one another during movement -- meaning, more than one continent can occupy a particular position simultaneously. If it is possible to form a square then you are also required to find the minimum number of moves to do that. The final position of the square is not the primary concern here. Note that in the final position, two continents cannot overlap and the desired square cannot have any holes in it. \InputFile The first line of input is an integer \textbf{T} (\textbf{T} ≤ \textbf{200}) that indicates the number of test cases. Each case consists of \textbf{20} lines containing \textbf{20} characters. Each character can either be a dot(.) representing an empty space or an \textit{\textbf{x}} (ASCII value \textbf{120}). Every \textit{\textbf{x}} character will be part of some continent. \textit{\textbf{Notes}}: \begin{itemize} \item In the input grid, it’s guaranteed that, \textit{\textbf{x}} characters from different continents will not be adjacent to each other. Any \textit{\textbf{x}} character will be part of some rectangle \item Two cells are adjacent if they share a common edge. \item There will be exactly \textbf{25} \textit{\textbf{x }}characters in the grid. \item The number of continents, \textbf{K}, will be at most \textbf{5}. \item There is a blank line before the start of every case. \item The rectangles and the desired square are axis parallel. \item The earth here is flat. That is, the first and last row is NOT adjacent to each other. Same thing holds for the first and last column. \end{itemize} \OutputFile For each case, output the case number first. If it’s not possible to form a square by moving the rectangles around, output “invalid data”, quotes for clarity. If it is possible, output the minimum number of moves required to complete the task.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2

....................
....................
....................
....................
....................
....................
..xxxxx.............
..xxxxx.............
....................
....................
..xx..........xxx...
....................
..xxxxx.............
..xxxxx.............
....................
....................
....................
....................
....................
....................

.................x..
....................
....................
....................
..xx................
..xx................
..xx................
..xx................
..xx................
..xx................
.......xxxxxx.......
.......xxxxxx.......
....................
....................
....................
....................
....................
....................
....................
....................
Вихідні дані #1
Case 1: 13
Case 2: invalid data
Джерело ACM-ICPC Asia Phuket Regional Programming Contest - 4 November 2011 – PSU, Phuket Campus