eolymp
bolt
Try our new interface for solving problems
Problems

Hexagonal Sticks

Hexagonal Sticks

In this problem, we will consider an infinite hexagonal grid. The grid consists of equal regular hexagonal cells arranged in the fashion shown below. The figure also elucidates the coordinate system used to identify each cell. Each cell of the grid can be empty or blocked. \includegraphics{https://static.e-olymp.com/content/fc/fc4392946d0b7e3f453b9bc09a28d87131ac92e3.jpg} Figure: A portion of the grid There are few sticks placed randomly on this grid. The length of each stick is one 'hexagonal unit'. This means, the end points of a stick lies on the centers of neighboring cells. Your job is to move the sticks so that a closed regular hexagonal figure is formed. The diagrams below show some closed hexagonal figures made of sticks. You will be given the initial coordinates of the sticks that are placed on the grid. You will also be given the coordinates of the cells that are blocked. In each move you can do one of the followings: \begin{itemize} \item Select one stick and throw it out \item Select one stick and rotate it \textbf{60} degrees clockwise/anti-clockwise about one of the end points \item Select one stick and push it along the length of the stick. \end{itemize} The sticks can never occupy a cell that is blocked. However, two sticks can occupy the same cells at the same time. \includegraphics{https://static.e-olymp.com/content/f3/f370d56009d79fcc415baed0b0456497a61e60e3.jpg} Consider the scenario shown above. We have an obstacle situated at coordinate (1, 1) and a stick at coordinate (0, 0) -> (1, 0). The four possible moves that can be made are depicted below After all the moves are made, you have to ensure a closed regular hexagonal shape is formed with no other sticks lying around. This means the grid should contain exactly \textbf{6*x} sticks where x is positive integer. Can you do this in \textit{minimum} number of moves? \InputFile The first line of input is an integer \textbf{T} (\textbf{T} < \textbf{50}) that indicates the number of test cases. Each case starts with a non-negative integer \textbf{S} (\textbf{S} < \textbf{9}) that gives you the number of sticks available. The next \textbf{S} lines give you the coordinates of the sticks. The coordinates of the sticks will be of the format \textbf{x_1 y_1 x_2 y_2}, which means there is a stick from \textbf{(x_1, y_1)}to \textbf{(x_2, y_2)}. The coordinates will be valid and the length of each stick will be one hexagonal unit as mentioned above. The next line will give you a non-negative integer \textbf{B} ( \textbf{B} < \textbf{20}) that indicates the number of obstacles. Each of the next B lines will give you the coordinates of the obstacles. The coordinate will be of the format \textbf{x_1 y_1}. It is guaranteed that the given blocks will not overlap with any given sticks. All the coordinates (sticks and blocks) will have values in the range \textbf{\[-4, 4\]}. Note: Remember that we are dealing with infinite grids. So in the optimal result, it could be possible that the hexagonal sticks lie outside \textbf{\[-4, 4\]}. \OutputFile For each case, output the case number first followed by the minimum number of moves required. If it is impossible to form a hexagonal grid, output "\textbf{impossible}" instead. Adhere to the sample input/output for exact format.
Time limit 1 second
Memory limit 256 MiB
Input example #1
3
6
-1 -1 -1 0
-1 0 0 1
0 1 1 1
1 1 1 0
1 0 0 -1
0 -1 -1 -1
0
5
-1 0 0 1
0 1 1 1
1 1 1 0
1 0 0 -1
0 -1 -1 -1
0
7
-2 -2 -2 -1
-1 -1 -1 0
0 0 1 1
0 0 1 1
0 0 1 1
1 1 2 2
1 2 2 2
2
1 0
2 0
Output example #1
Case 1: 0
Case 2: impossible
Case 3: 9