Hexagonal Sticks
Time limit1sMemory limit128 MB
Given at most 8 unit sticks on an infinite hexagonal grid with blocked cells, find the minimum number of moves (rotate, push, or discard) so the sticks form one closed regular hexagon.
- Level
Hard8 of 10
- Topics
- BFS, Brute force, Geometry, Implementation
- Solved
- No attempts yet
Problem
Consider an infinite hexagonal grid made of identical regular hexagonal cells arranged as shown below. The figure also shows the coordinate system used to identify each cell. Every cell is either empty or blocked.

Figure: A portion of the grid
A number of sticks are placed on the grid. Each stick has a length of one hexagonal unit: its two endpoints lie on the centers of two neighbouring cells. Your task is to move the sticks so that they form a single closed regular hexagon. The pictures below show some closed hexagonal figures built from sticks.
You are given the initial coordinates of the sticks together with the coordinates of the blocked cells. In one move you may do exactly one of the following:
- pick a stick and throw it away;
- pick a stick and rotate it 60° clockwise or anti-clockwise about one of its endpoints;
- pick a stick and push it one unit along its own length.
A stick may never occupy a blocked cell. Two sticks, however, are allowed to occupy the same cells at the same time.

Consider the situation above: a blocked cell at (1, 1) and a stick from (0, 0) to (1, 0). The four possible moves are shown below.
After all moves are made, the remaining sticks must form exactly one closed regular hexagon with no leftover sticks. In other words the grid must contain exactly sticks for some positive integer , and they must form one closed hexagonal outline. Find the minimum number of moves needed.
Input
The first line contains an integer (), the number of test cases. Each test case begins with a non-negative integer (), the number of sticks. Each of the next lines contains four integers x1 y1 x2 y2, describing a stick from to ; every stick has length exactly one hexagonal unit. The next line contains a non-negative integer (), the number of blocked cells, followed by lines each containing two integers x y for one blocked cell. No blocked cell coincides with a stick. All given coordinates lie in the range .
Note: the grid is infinite, so in an optimal solution the final hexagon may use cells outside .
Output
For each test case print one line in the form Case i: m, where is the test-case number (starting from 1) and is the minimum number of moves required. If it is impossible to form a closed hexagon, print Case i: impossible instead.






