Connection Status:
Competition Arena > EscapeFromRectangle
SRM 311 · 2006-07-12 · by gevak · Simple Math
Class Name: EscapeFromRectangle
Return Type: int
Method Name: shortest
Arg Types: (int, int, int, int)
Problem Statement

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.

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
0)
1
1
5
5
Returns: 1

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

1)
6
2
10
3
Returns: 1

Go straight to the upper boundary here.

2)
653
375
1000
1000
Returns: 347

Going straight to the right leads to success.

3)
49
26
77
53
Returns: 26
4)
15
13
65
15
Returns: 2

Submissions are judged against all 25 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class EscapeFromRectangle with a public method int shortest(int x, int y, int w, int h) · 25 test cases · 2 s / 256 MB per case

Submitting as anonymous