Hexagonal Tile Maze Escape
InterviewTime limit1sMemory limit1024 MB
Find the shortest path from (0,0) to (N-1, M-1) on a hex grid avoiding obstacles, and print its tile count or -1.
Section 1
Yunsu fell into a hexagonal tile maze on the way to a party, after being tricked by the nasty Jonghyun. The maze is a grid of regular hexagonal tiles with rows and columns. Compare two cells in the same column: a cell in an even-numbered row is half a cell to the right of a cell in an odd-numbered row. The figure below shows a hexagonal tile maze with and .

Yunsu always starts at , and the exit is always at . Jonghyun placed obstacles on the tiles to block Yunsu's escape. On a tile, Yunsu can move to an adjacent tile, but cannot move onto a tile that has an obstacle. Two tiles are adjacent if they share a side.
Yunsu is in a hurry to get to the party. A shortest path is a path to the exit that passes through the fewest tiles. The starting tile is not counted, and the exit tile is counted. Find the number of tiles on the shortest path from the start to the exit.
Input
The first line contains the maze size and and the number of obstacles , separated by spaces.
From the second line, the next lines each contain the position of an obstacle , separated by a space. No two obstacles are at the same position. The starting tile and the exit tile never have an obstacle.
Output
Print the number of tiles on the shortest path from the start to the exit. If Yunsu cannot reach the exit, print -1.
Limits
- All numbers in the input are integers.