Illumination
Time limit1sMemory limit256 MB
Count the hexagon edges of occupied cells that border open space reachable from outside the map, excluding walls around enclosed courtyards.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Implementation, Simulation
- Solved
- No attempts yet
Problem
Sanggeun's house is a single building made of many regular hexagons, each with a side length of 1 metre, packed together like a honeycomb. For Christmas he wants to hang lights only on the walls that are visible from outside the building, because lighting walls that cannot be seen from the outside would be a waste.
The building sits on a hexagonal grid. Each cell is either occupied by the building or empty. A wall that can be lit is one of the six edges of an occupied cell that touches outside space. The area outside the map is open space you can move through freely, but you cannot pass between two occupied cells that share an edge. Consequently, walls that face an empty region fully enclosed by the building (a courtyard) and therefore not connected to the outside are NOT lit.
Given the building layout, write a program that computes the total length of the walls to be lit. Each edge of a hexagon is 1 metre long.
Input
The first line contains two integers and . ()
Each of the next lines describes the layout. The -th line from the top contains integers separated by spaces; the -th integer on that line describes the cell at coordinate . A value of means the cell is occupied by the building and means it is empty. At least one cell is occupied.
Coordinates on the hexagonal grid follow these rules.
- The top-left hexagon has coordinate .
- The hexagon to the right of has coordinate .
- When is odd, the hexagon directly below has coordinate .
- When is even, the hexagon to the lower-right of has coordinate .
Output
Print the total length of the walls to be lit on a single line. Since every edge is 1 metre long, the answer equals the number of walls that touch the outside.