PROBLEM STATEMENT
You are given a sheet of paper with a given height and width. You are also
given a set of rectangles to shade in on the paper. Your job is to return the
total area on the paper covered by the rectangles drawn on the paper. The top
left corner of the paper has coordinates <0,0>, and the bottom right corner of
the paper has coordinates <width,height>.
Create a class Shade that contains the method area, which takes as parameters
the height and width of the paper, and a String[] coordinates, with each
element of coordinates containing two diagonally opposed points of a shaded
rectangle. This method returns the total area shaded on the paper.
Here is an example of paper with width = 2 and height = 3. Rectangle "0,0,1,2"
is shaded with '*'.
(0,0) ___________ (2,0)
| * * | |
|* * *| |
| * * | |
|* * *| |
|_*_*_| |
(0,2)| (1,2) |
| |
|___________|
(0,3) (2,3)
DEFINITION
Class: Shade
Method: area
Parameters: int, int, String[]
Returns: int
Method signature (be sure your method is public): int area(int height, int
width, String[] coordinates);
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- height and width are ints between 1 and 100 (inclusive)
- coordinates will contain between 1 and 50 rectangles (inclusive)
- coordinates will be formatted "x1,y1,x2,y2" where the point <x1,y1> is one
corner of a rectangle, and <x2,y2> is the opposing corner (the two points form
a diagonal for the rectangle), with ',' separating the coordinates, and no
spaces will exist in the String
- each element of coordinates will have length between 1 and 50, inclusive
- a rectangle's x1 and x2 coordinates are between 0 and width, inclusive
- a rectangle's y1 and y2 coordinates are between 0 and height, inclusive
- x1 will not equal x2, y1 will not equal y2
- coordinates may have leading zeros, so "01" is allowed
EXAMPLES
1)
height = 10
width = 10
coordinates = {"0,0,01,1","1,1,5,6","0,1,4,5"}
Method returns 25
The area "1,1,4,5" is shaded twice, but only gets counted once. There are 21
area units contained in the first two rectangles, and 4 contained in the
non-overlapping area of the third rectangle.
2)
height = 10
width = 10
coordinates = {"0,0,1,1","1,1,5,6"}
Method returns 21
3)
height = 100
width = 100
coordinates = {"0,0,10,15","3,18,54,68","36,87,20,68"}
Method returns 3004
4)
height = 100
width = 100
coordinates = {"0,0,1,1","1,1,5,6","1,100,3,79"}
Method returns 63