PROBLEM STATEMENT
There are bombs located at various points in the city. We want to set up a
rectangular perimeter running east-west and north-south which contains all the
bombs. Create a class Surround that contains the method perimeter that takes a
int[] xBomb and a int[] yBomb, representing the coordinates of the bombs as
inputs and returns the length of the smallest possible perimeter that surrounds
all the bombs.
Each bomb must be inside the perimeter -- it is not allowable to have a bomb on
the perimeter. We will only consider perimeters that have integer coordinates
at their corners.
DEFINITION
Class: Surround
Method: perimeter
Parameters: int[], int[]
Returns: int
Method Signature (be sure your method is public): int perimeter(int[] xBomb,
int[] yBomb);
NOTES
- the corresponding elements in xBomb and yBomb refer to the same bomb. For
example, the coordinates of the first bomb are xBomb[0],yBomb[0]
- x indicates distance east, and y indicates distance north
- there may be more than one bomb at the same location
- the perimeter must be rectangular, with horizontal and vertical sides
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- xBomb will have between 1 and 50 inclusive elements
- yBomb will have the same number of elements as xBomb
- each element in xBomb will be between -1000 and 1000 inclusive
- each element in yBomb will be between -1000 and 1000 inclusive
EXAMPLES
1) {3,5,5},{7,6,7}: return 14
9 +---+---+---+---+---+---+---+
| | | | | | | |
8 +---XXXXXXXXXXXXXXXXX---+---+
| X | | | X | |
7 +---X---B---+---B---X---+---+
| X | | | X | |
6 +---X---+---+---B---X---+---+
| X | | | X | |
5 +---XXXXXXXXXXXXXXXXX---+---+
| | | | | | | |
4 +---+---+---+---+---+---+---+
1 2 3 4 5 6 7 8
B shows the location of a bomb. X shows the desired perimeter. The length of
each east-west side is 4 (from x=2 to x=6) and the length of each north-south
side is 3 (from y=5 to y=8). 4+3+4+3 = 14
2) {-3,5,0}, {4,-2,1}: return 36
The 3 bombs are located at (-3,4),(5,-2) and (0,1). The smallest perimeter has
two of its corners at x=-4,y=-3 and at x=6,y=5. The perimeter of this
rectangle consists of 4 lines with length = 10+8+10+8.
3) {-1000,-1000,-1000,-1000}, {1000,1000,1000,1000]: return 8
There are 4 bombs at the same location. The perimeter will have -1001,999 and
-999,1001 as two of its corners. It is a square with each side of length 2.
4) {1,2,3,4}, {1,5,0,1}: return 24