Treasure Map
Time limit1sMemory limit128 MB
Given a disk, a boundary start point, and an interior target, find the minimum total length of axis-aligned or 45-degree steps that stay inside the disk.
- Level
Hard8 of 10
- Topics
- Geometry, Math, Shortest path, Greedy
- Solved
- No attempts yet
Problem
A pirate's treasure map is a list of instructions that lead from where you land on a desert island to the spot marked X, where the treasure is buried. Your task is to find the shortest such walk on one particular island.
The island is a disk of radius paces centred at the origin . Relative to the centre, is north, is south, is east, and is west; likewise is northeast, is southeast, is northwest, and is southwest.
You land on the shore (a point on the boundary circle), and the spot marked X lies somewhere on the island. A single instruction has the form
direction distance
where direction is one of north, south, east, west, northeast, northwest, southeast, southwest, and distance is the number of paces walked in that direction. Walking northeast for a distance , for example, moves you paces east and paces north.
Consider every sequence of instructions that leads from the landing place to the spot marked X without ever leaving the island. Among all such sequences, report the smallest possible total distance walked (the sum of the distances of the individual instructions).
Input
The input contains several test cases. Each test case is a line with five integers , , , , : the radius of the island, the coordinates of the landing place, and the coordinates of the spot marked X. The landing place lies on the shore, so , and the spot lies on the island, so . The landing place and the spot marked X are distinct. A line containing a single -1 follows the last test case.
Output
For each test case, print one line with the minimum possible total distance, rounded to exactly six digits after the decimal point.