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
5
5
Returns: 1
We can go either straight down or straight to the left.
6
2
10
3
Returns: 1
Go straight to the upper boundary here.
653
375
1000
1000
Returns: 347
Going straight to the right leads to success.
49
26
77
53
Returns: 26
15
13
65
15
Returns: 2
17
9
30
20
Returns: 9
40
81
80
120
Returns: 39
287
148
400
300
Returns: 113
66
401
200
860
Returns: 66
1
1
1000
1000
Returns: 1
500
500
1000
1000
Returns: 500
130
570
654
618
Returns: 48
57
120
720
328
Returns: 57
91
466
773
667
Returns: 91
341
348
643
353
Returns: 5
161
181
762
375
Returns: 161
398
371
526
915
Returns: 128
423
504
442
528
Returns: 19
158
3
192
18
Returns: 3
94
25
134
44
Returns: 19
53
6
370
60
Returns: 6
3
4
5
5
Returns: 1
1
1
5
5
Returns: 1
3
9
10
10
Returns: 1
100
1
200
1000
Returns: 1