Cow Rescue
Time limit1sMemory limit128 MB
Find the minimum walking time from a start triangle to any exit in a triangular maze with up to a million rows, then break ties by row and column.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Math, Geometry
- Solved
- No attempts yet
Problem
Bessie is trapped in a triangular maze with rows (). Row of the maze contains triangles. Numbering from the left, the triangles in row are named .
Each triangle shares an edge with (usually three) neighboring triangles, and Bessie can move to any triangle that shares an edge with her current one. For example, from triangle she can move to , , and . Each move takes exactly minute.
(Triangles in odd columns point upward and those in even columns point downward. An upward triangle is adjacent to , , and ; a downward triangle is adjacent to , , and . Neighbors outside the maze do not exist.)
Bessie starts at triangle . The maze has exit triangles (). Reaching any exit lets Bessie escape: once she enters an exit triangle, she leaves the maze after one more minute.
Let be the minimum number of minutes Bessie needs to escape. Among all exits that let her escape in exactly minutes, report the one she should use: choose the exit in the smallest row, and if there is still a tie, the exit with the smallest column.
Input
- Line 1: two space-separated integers and .
- Line 2: two space-separated integers and — Bessie's starting triangle.
- Lines 3 through : line contains two space-separated integers and , the location of exit .
Output
- Line 1: two space-separated integers and — the chosen exit location.
- Line 2: a single integer — the minimum escape time in minutes.