ManhattanMovement
SRM 226 · 2005-01-05 · by the_one_smiley
Problem Statement
You are in a big flat city, and wish to travel to a road. The road is an infinite straight line that may have any orientation, but you are restricted to only moving in the four cardinal directions. If the city is represented by the xy-plane, your starting location is the point (x0, y0) and the road is the line a*x + b*y = 1. You can change directions at any point and travel as far as you want in each direction, but you can only travel parallel to the x or y-axis. Write a class ManhattanMovement with a method getDistance that takes four
Notes
- A return value with either an absolute or relative error of less than 1e-9 is considered correct.
Constraints
- a, b, x0, and y0 will all be between -2147483648 and 2147483647, inclusive.
- a and b will not both equal zero.
1 2 -2 3 Returns: 1.5
Moving straight south (in the negative y-direction) yields the shortest distance.
37 37 42 19 Returns: 60.97297297297297
Moving either straight south or straight west yields the same minimum distance.
-100 0 -999999 314159 Returns: 999998.99
0 -2147483648 1 100000 Returns: 100000.00000000047
-2147483648 2147483647 2147483647 2147483647 Returns: 1.0
Submissions are judged against all 70 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ManhattanMovement with a public method double getDistance(int a, int b, int x0, int y0) · 70 test cases · 2 s / 256 MB per case