TopcoderArchiveVisit Topcoder
SRM12 Jul 2006
SRM 311

EscapeFromRectangle

Problem statement, definition, constraints, and public examples.

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. 161
    181
    762
    375
    Returns: 161
Back to all problems