Honey Heist
InterviewTime limit2sMemory limit512 MB
Build the adjacency of a hexagonal honeycomb of side R, remove wax cells, and find the shortest number of chewed cells from A to B, checking it against N.
- Level
Medium6 of 10
- Topics
- Graph, BFS, Implementation, Geometry
- Solved
- No attempts yet
Problem
The scout ant 0x67 is looking for food and finds a beehive nearby. Inside the comb there is a cell packed with dried honey, and carrying that honey back to the nest would feed the colony through the winter. To reach the honey, 0x67 has to chew a tunnel through the cells of the comb.
The comb is a regular hexagon whose every edge is cells long, so it holds cells. The cells are numbered in row major order: rows from top to bottom, and within a row from left to right. Two cells are adjacent when they share a wall. The numbering for is:

0x67 goes through the hole and lands in cell . It does not chew that cell. From there it chews one adjacent cell at a time on its way to cell , which holds the honey. Its jaw gives out after a while, so it can chew at most cells. Some cells are hardened with wax and cannot be chewed, so 0x67 has to work around them.
Before it starts, 0x67 computes , the smallest number of cells it has to chew to reach cell , so that is the th cell it chews. If , it is not strong enough to dig the tunnel.
Input
The first line contains five integers , , , , separated by spaces.
- : the number of cells along each edge of the comb (). The comb holds cells.
- : the largest number of cells 0x67 can chew ().
- : the ID of the cell 0x67 enters. This cell lies on the border of the comb, so it has fewer than six neighbors.
- : the ID of the cell that holds the honey ().
- : the number of wax hardened cells ().
The second line contains the IDs of the wax hardened cells, separated by spaces. When the second line is empty.
, and the IDs on the second line are all different, and each of them is an integer between and .
Output
If 0x67 can reach the honey, print , the smallest number of cells it has to chew. If the wax seals off cell or , print No instead.
Hint

This is the comb of the first example. The black cells are hardened with wax, and the six green cells are the ones 0x67 chews.

The second example uses the same comb with . After three red cells the jaw gives out, so the honey stays out of reach.