SP2052 CERC07H - Hexagonal Parcels
Description
A civil engineer that has recently graduated from the Czech Technical University encountered an interesting problem and asked us for a help. The problem is more of economical than engineering nature. The engineer needs to connect several buildings with an infrastructure. Unfortunately, the investor is not the owner of all the land between these places. Therefore, some properties have to be bought first.
The land is divided into a regular "grid" of hexagonal parcels, each of them forms an independent unit and has the same value. Some of the parcels belong to the investor. These parcels form four connected areas, each containing one building to be connected with the others. Your task is to find the minimal number of parcels that must be acquired to connect the four given areas.
The whole land also has a hexagonal shape with six sides, each consisting of exactly H parcels. The above picture shows a land with H = 4, parcels with letters represent the four areas to be connected. In this case, it is necessary to buy four additional parcels. One of the possible solutions is marked by crosses.
Input Format
The input contains several scenarios. Each scenario begins with an integer number H,which specifies the size of the land, 2
Output Format
For each scenario, output one line with the sentence "You have to buy P parcels.", where P is the minimal number of parcels that must be acquired to make all four areas connected together.
Areas are considered connected, if it is possible to find a path between them that leads only through parcels that have been bought.