Statistics

Problem Statement for "EscapeFromRectangle"

Problem Statement

You are currently at a point (x,y) inside a rectangle. The rectangle's bottom left corner is at (0,0) and its top right corner is at (w,h). Return the shortest distance you must travel in order to reach the rectangle's boundary.

Definition

Class:
EscapeFromRectangle
Method:
shortest
Parameters:
int, int, int, int
Returns:
int
Method signature:
int shortest(int x, int y, int w, int h)
(be sure your method is public)

Constraints

  • w and h will be between 2 and 1000, inclusive.
  • x will be between 1 and w - 1, inclusive.
  • y will be between 1 and h - 1, inclusive.

Examples

  1. 1

    1

    5

    5

    Returns: 1

    We can go either straight down or straight to the left.

  2. 6

    2

    10

    3

    Returns: 1

    Go straight to the upper boundary here.

  3. 653

    375

    1000

    1000

    Returns: 347

    Going straight to the right leads to success.

  4. 49

    26

    77

    53

    Returns: 26

  5. 15

    13

    65

    15

    Returns: 2

  6. 17

    9

    30

    20

    Returns: 9

  7. 40

    81

    80

    120

    Returns: 39

  8. 287

    148

    400

    300

    Returns: 113

  9. 66

    401

    200

    860

    Returns: 66

  10. 1

    1

    1000

    1000

    Returns: 1

  11. 500

    500

    1000

    1000

    Returns: 500

  12. 130

    570

    654

    618

    Returns: 48

  13. 57

    120

    720

    328

    Returns: 57

  14. 91

    466

    773

    667

    Returns: 91

  15. 341

    348

    643

    353

    Returns: 5

  16. 161

    181

    762

    375

    Returns: 161

  17. 398

    371

    526

    915

    Returns: 128

  18. 423

    504

    442

    528

    Returns: 19

  19. 158

    3

    192

    18

    Returns: 3

  20. 94

    25

    134

    44

    Returns: 19

  21. 53

    6

    370

    60

    Returns: 6

  22. 3

    4

    5

    5

    Returns: 1

  23. 1

    1

    5

    5

    Returns: 1

  24. 3

    9

    10

    10

    Returns: 1

  25. 100

    1

    200

    1000

    Returns: 1


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: