Airways
SRM 267 · 2005-10-05 · by dgoodman
Problem Statement
It makes sense to distribute the allowable directions evenly. If we allow only n directions, one will be east and the others will be evenly distributed among all other possible directions. So if n is 3, the three directions are east, 30 degrees west of north, and 30 degrees west of south. Adjacent directions then differ by 120 degrees. Notice that if a direction is legal the opposite direction may not necessarily be legal.
The following picture shows a minimum way to fly to a destination which is 5 east and 3 north of the starting point when n is 8. The 8 allowable directions are shown in blue. By flying a distance of 3*sqrt(2) northeast, and then 2 east (or 2 east, and then 3*sqrt(2) northeast) we can get to our destination by travelling a total distance of 3*sqrt(2) + 2. Notice that for any n and destination the minimal distance can be achieved using no more than two directions.
Create a class Airways that contains a method distance that is given n (the number of legal directions), east (the distance eastward to our destination), and north (the distance northward to our destination). It returns the minimum distance we will have to fly to get to our destination under these requirements.
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- n will be between 3 and 40, inclusive.
- east and north will be between -5000 and 5000, inclusive.
3 -219 0 Returns: 437.99999999999994
We want to go 219 due west. One way to do that is to fly 219 in the direction that is 30 degrees west of due south, then fly 219 in the direction 30 degrees west of due north. Our path will form 2 sides of an equilateral triangle.
3 171 0 Returns: 171.0
We want to go due east, and that is always an allowed direction.
4 233 3111 Returns: 3344.0000000000005
We can go 3111 due north and then 233 east since these are legal directions when n is 4.
40 -2912 -487 Returns: 2954.3363333516754
40 -66 -66 Returns: 93.3380951166242
Submissions are judged against all 52 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Airways with a public method double distance(int n, int east, int north) · 52 test cases · 2 s / 256 MB per case