Statistics

Problem Statement for "ManhattanMovement"

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 ints a, b, x0, and y0 and returns the shortest distance you must travel in order to reach the road.

Definition

Class:
ManhattanMovement
Method:
getDistance
Parameters:
int, int, int, int
Returns:
double
Method signature:
double getDistance(int a, int b, int x0, int y0)
(be sure your method is public)

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.

Examples

  1. 1

    2

    -2

    3

    Returns: 1.5

    Moving straight south (in the negative y-direction) yields the shortest distance.

  2. 37

    37

    42

    19

    Returns: 60.97297297297297

    Moving either straight south or straight west yields the same minimum distance.

  3. -100

    0

    -999999

    314159

    Returns: 999998.99

  4. 0

    -2147483648

    1

    100000

    Returns: 100000.00000000047

  5. -2147483648

    2147483647

    2147483647

    2147483647

    Returns: 1.0

  6. -2147483648

    2147483647

    -2147483648

    2147483647

    Returns: 4.294967294E9

  7. 0

    1266607226

    319501161

    -1683299080

    Returns: 1.68329908E9

  8. 0

    -1280895397

    -619845035

    -2129647650

    Returns: 2.12964765E9

  9. 1552476727

    0

    2113262842

    -506687009

    Returns: 2.113262842E9

  10. -217347990

    1407742898

    -664375507

    0

    Returns: 1.0257603235422675E8

  11. -1113346878

    -1021908798

    0

    -646591447

    Returns: 5.934875297695411E8

  12. 1062627157

    1843502487

    -1303271978

    -2104708680

    Returns: 2.8559374993511434E9

  13. 0

    -317229867

    1987274807

    -1316239511

    Returns: 1.316239511E9

  14. 1397065508

    0

    1835962049

    1922408784

    Returns: 1.835962049E9

  15. -1568147842

    649492931

    -544210970

    0

    Returns: 5.4421097E8

  16. -851399272

    0

    -762401940

    559901120

    Returns: 7.6240194E8

  17. -457189178

    0

    0

    1126486756

    Returns: 2.187278369918021E-9

  18. -169740581

    2035575761

    924505906

    -2073207966

    Returns: 2.1502997513459206E9

  19. 0

    -1804341624

    0

    -297546872

    Returns: 2.97546872E8

  20. 887352994

    0

    -1593631248

    -1800776176

    Returns: 1.593631248E9

  21. 874039624

    -1736181805

    109277531

    -2102562332

    Returns: 2.1575755177342486E9

  22. 2126901224

    0

    -1402314328

    -1897487320

    Returns: 1.402314328E9

  23. 1937131179

    1069415688

    0

    1477380511

    Returns: 8.156050105106777E8

  24. -780521089

    -45114916

    300425672

    1277533513

    Returns: 3.7426841364843214E8

  25. -719544233

    -845290711

    1369238103

    -840333872

    Returns: 3.252146993926294E8

  26. 1657188665

    989362243

    0

    0

    Returns: 6.034315954001532E-10

  27. 0

    -2130809015

    0

    -514714893

    Returns: 5.14714893E8

  28. -411639651

    0

    727041475

    -1528538211

    Returns: 7.27041475E8

  29. 1760499823

    1361204101

    510990818

    -1685336722

    Returns: 7.920977864867821E8

  30. 594400399

    711893226

    -1405775321

    -929447020

    Returns: 2.103209293876101E9

  31. 0

    -1486085067

    -946121651

    -689273609

    Returns: 6.89273609E8

  32. 912964595

    972752948

    0

    0

    Returns: 1.028010248702942E-9

  33. 994056210

    -1393416347

    10381251

    132111755

    Returns: 1.2470582277713172E8

  34. 2007302364

    -308112422

    -1413532684

    1674579412

    Returns: 1.6705735402836006E9

  35. 565185771

    0

    1013180833

    0

    Returns: 1.013180833E9

  36. -1853480675

    1984551007

    -101694228

    0

    Returns: 9.49777988533423E7

  37. 1535479008

    1051733211

    -173568633

    -488466343

    Returns: 5.081458383621051E8

  38. 270737482

    1297953176

    1930820191

    792132870

    Returns: 1.194878829077725E9

  39. 0

    -303512880

    600120122

    1874317933

    Returns: 1.874317933E9

  40. 1831418372

    1525022416

    -328149196

    -1630067958

    Returns: 1.6855070851417534E9

  41. 1784551678

    -1944783475

    0

    -1520703866

    Returns: 1.520703866E9

  42. 0

    -156171033

    -671173395

    944139506

    Returns: 9.44139506E8

  43. 0

    -914266118

    -899831263

    253347829

    Returns: 2.53347829E8

  44. -768103699

    1140575222

    1301332296

    683749690

    Returns: 1.926135089418782E8

  45. -866525973

    -1572986981

    0

    -981092426

    Returns: 9.81092426E8

  46. 2095200530

    942570169

    1871234403

    1323239166

    Returns: 2.4665214633388205E9

  47. 1116085719

    942270053

    2124143449

    0

    Returns: 2.124143449E9

  48. -1884084662

    1768542953

    -43946883

    -6778570

    Returns: 3.758400958634381E7

  49. -663959488

    571119000

    655157794

    -530028964

    Returns: 1.1110735800708454E9

  50. 1514025619

    -1491915732

    -1666374390

    999601249

    Returns: 2.651378085046424E9

  51. -100

    0

    -999999

    314159

    Returns: 999998.99

  52. 2000000000

    2000000000

    2000000000

    2000000000

    Returns: 4.0E9

  53. 0

    1

    1

    1

    Returns: 0.0

  54. -2147483648

    -2147483648

    2147483647

    2147483647

    Returns: 4.294967294E9

  55. 100

    -10000000

    1

    -10000000

    Returns: 1.00000000000099E7

  56. 2

    -2

    2000000000

    2000000000

    Returns: 0.5

  57. 2147483647

    2147483647

    2147483647

    2147483647

    Returns: 4.294967294E9

  58. 30000

    30000

    100000

    100000

    Returns: 199999.99996666668

  59. 123456789

    123456789

    123456789

    123456789

    Returns: 2.46913578E8

  60. 348736273

    348326273

    332736273

    328736273

    Returns: 6.610860594703065E8

  61. -2147483648

    -2147483648

    2147483647

    -2147483648

    Returns: 1.0

  62. 50000

    50000

    50000

    50000

    Returns: 99999.99998

  63. 0

    -5

    -1

    -4

    Returns: 3.8

  64. 1

    0

    1

    0

    Returns: 0.0

  65. 0

    -2147483648

    1

    100000

    Returns: 100000.00000000047

  66. 3000

    3000

    1000000

    1000000

    Returns: 1999999.9996666666

  67. 10

    1

    0

    -100

    Returns: 10.1

  68. 1

    33

    4

    5

    Returns: 5.090909090909091

  69. -2147483648

    -2147483648

    -2147483648

    -2147483648

    Returns: 4.294967296E9

  70. 34534344

    342432

    10000000

    45433443

    Returns: 1.0450504134474799E7


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: