Hexagonal Pasture Network
InterviewTime limit1sMemory limit128 MB
Given a hexagon-shaped grid of pastures, count and list all pastures at exact distance L from start H using BFS.
- Level
Medium5 of 10
- Topics
- Graph, BFS, Implementation
- Solved
- No attempts yet
Problem
Farmer John recently acquired some new land to expand his farm. His cows have taken a liking to the hexagonal structure of bee honeycombs, so Farmer John has laid out a new system of pastures and cowpaths in that shape.
The whole plot of pastures and cowpaths forms a hexagon with side length (). The pastures are numbered .
Think of the hexagon as a set of vertical columns. From left to right the columns contain pastures (the middle column is the tallest, with pastures). Pastures are numbered consecutively starting from : column by column from left to right, and within each column from bottom to top. So pasture sits at the bottom of the leftmost column, and the last pasture sits at the top of the rightmost column.
Each pasture is connected by a cowpath to every one of its immediate neighbors. A pasture in the interior of the hexagon is adjacent to exactly six others; for example, when , pasture is adjacent to pastures , , , , , and . A pasture on an edge (but not a corner) is adjacent to exactly four others (e.g. pasture is adjacent to , , , and ), and a pasture at a corner is adjacent to only three (e.g. pasture is adjacent to , , and ). Every cowpath has length , and the distance between two pastures is the length of the shortest route between them.
Farmer John's Holstein cows have been grazing in pasture () for several days and have grown fat and lazy. To make them exercise, Farmer John places tasty treats in every pasture that is exactly at distance () from the cows. He promises that at least one treat has been placed, but he does not reveal which pastures hold them.
Help the cows avoid unnecessary walking: determine how many pastures could hold a treat, and list them in ascending order.
Input
The first line contains three space-separated integers , , and .
Output
On the first line, print a single integer: the number of pastures that are exactly at distance from pasture .
On each of the following lines, print one such pasture number, in ascending order.