Problem Statement |
|
You are given a black and white image in a String[], image. Character j of element i (both 0-based indices) of image represents the pixel in row i, column j. ‘X’ characters represent black pixels and ‘.’ characters represent white pixels. You are given a String[], crops, which contains a series of rectangular crop operations that are performed on the image. Cropping is an operation that trims an image so that only the specified area of the original image remains. Each element of crops is formatted as “r1 c1 r2 c2″ (quotes for clarity only), where the upper left corner of the area to crop is at row r1, column c1, and the lower right corner is at row r2, column c2. The coordinates are inclusive. The crop operations are performed in the order that they appear in crops, and each one is performed on the most recent version of the image. The constraints will guarantee that all crop operations will be within the boundaries of the image. Return the final cropped image as a String[] in the same format as the original image String[]. |
Definition |
|
Class: |
Crop |
Method: |
crop |
Parameters: |
String[], String[] |
Returns: |
String[] |
Method signature: |
String[] crop(String[] image, String[] crops) |
(be sure your method is public) |
|
|
|
Constraints |
- |
image will contain between 1 and 50 elements, inclusive. |
- |
Each element of image will contain between 1 and 50 characters, inclusive. |
- |
Each element of image will contain exactly the same number of characters. |
- |
Each element of image will contain only ‘.’ and uppercase ‘X’ characters. |
- |
crops will contain between 1 and 10 elements, inclusive. |
- |
Each element of crops will be formatted as described in the problem statement and no integers within crops will contain extra leading zeros. |
- |
Within each element of crops, r2 will be greater than or equal to r1 and c2 will be greater than or equal to c1. |
- |
crops will contain no operations that exceed the boundaries of the image at any time in the cropping process. |
Examples |
0) |
|
|
{“………”,
“X.XXXXXXX”,
“….X….”,
“………” } |
{“1 0 2 8″, “0 0 1 1″} |
|
Returns: {“X.”, “..” } |
The first crop effectively removes the top and bottom rows of the image and results in:
X.XXXXXXX
….X….
The second crop is then performed relative to the new image:
X.
.. |
|
|
1) |
|
|
{“X.X.X.X.X.X.X.X”,
“.X.X.X.X.X.X.X.”} |
{“0 0 1 14″, “0 0 1 14″, “0 0 1 14″} |
|
Returns: {“X.X.X.X.X.X.X.X”, “.X.X.X.X.X.X.X.” } |
These crops don’t affect the original image at all. |
|
|
2) |
|
|
{“.X..X.X.XX.”,
“..X..X…X.”,
“X……X..X”,
“.X….X…X”,
“..XXXX.X.X.”,
“XXX..XXX..X”} |
{“0 0 0 0″} |
|
Returns: {“.” } |
|
|
3) |
|
|
{“.X..X.X.XX.”,
“..X..X…X.”,
“X……X..X”,
“.X….X…X”,
“..XXXX.X.X.”,
“XXX..XXX..X”} |
{“1 0 5 9″, “0 1 4 8″, “0 0 3 5″} |
|
Returns: {“.X..X.”, “……”, “X….X”, “.XXXX.” } |
|
|