Problem Statement
You are given the upper left corner and side length of a square selection on a 20 x 20 pixel canvas. You are also given a target location on the same canvas. Execute a cut command on the initial square selection and then execute a paste command at the target location. A cut command puts all the pixels in the selected area into memory and replaces them on the canvas with blank pixels. A paste command places all the pixels in memory onto the canvas, replacing whatever pixels were initially there. The target coordinate is the upper left corner of where the pasted selection will end up. Pixels that lie outside of the 20 x 20 canvas after the paste operation will be cropped. The canvas is given as a String[], canvas, where character j of element i (both 0-indexed) represents the pixel at (i, j). Black pixels are represented as uppercase 'X' characters and blank pixels are represented as '.' characters. The square selection's upper left corner is at (r, c), and its side length is length. The target location's upper left corner is at (targetR, targetC). Return the canvas after the paste command as a String[] in the same format as canvas.
Definition
- Class:
- CutAndPaste
- Method:
- execute
- Parameters:
- String[], int, int, int, int, int
- Returns:
- String[]
- Method signature:
- String[] execute(String[] canvas, int r, int c, int length, int targetR, int targetC)
- (be sure your method is public)
Constraints
- canvas will contain exactly 20 elements.
- Each element of canvas will contain exactly 20 characters.
- Each character in each element of canvas will be either '.' or uppercase 'X'.
- r, c, targetR, and targetC will each be between 0 and 19, inclusive.
- length will be between 1 and 20, inclusive.
- r+length-1 and c+length-1 will each be between 0 and 19, inclusive.
Examples
{"..XXXXXXXX.X.X.XXXXX", ".X.XXXXXXXX.X.X.XXXX", "XXXXXXXXXX.X.XXXXXXX", ".XXX.XXXXXX.X.X.XXXX", "XXXXXXXXXX.X.X.XXXXX", "XXXXXXXXXXXXX.XXXXXX", "XXXXXXXXXXXX.X.XXXXX", "XXXXXXXXXXX.X.XXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXX.XXXXXXXXXXXXX", ".X.X.XXXXXXXXXXXXXXX", "X.X.XXX.X.X.X.XXXXXX", "XX.XXXXX.X.X.X.XXXXX", "X.X.XXXXX.X.XXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX" }0
0
10
5
5
Returns: {"...........X.X.XXXXX", "..........X.X.X.XXXX", "...........X.XXXXXXX", "..........X.X.X.XXXX", "...........X.X.XXXXX", ".......XXXXXXXXXXXXX", "......X.XXXXXXXXXXXX", ".....XXXXXXXXXXXXXXX", "......XXX.XXXXXXXXXX", ".....XXXXXXXXXXXXXXX", ".X.X.XXXXXXXXXXXXXXX", "X.X.XXXXXXXXXXXXXXXX", "XX.XXXXXXXXXXXXXXXXX", "X.X.XXXXXXXXXXXXXXXX", "XXXXXXXXXXX.XXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX" }{"....................", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXXXXX.....", "..XXXXXXXXXXXXX.....", "..XXXXXXXXXXXXX.....", "..XXXXXXXXXXXXXXXXX.", "..XXXXXXXXXXXXXXXXX.", "..XXXXXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".........XXXXXXXXXX.", ".........XXXXXXXXXX.", ".........XXXXXXXXXX.", "....................", "...................." }8
8
1
9
9
Returns: {"....................", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXX........", "..XXXXXXXXXXXXX.....", "..XXXXXXXXXXXXX.....", "..XXXXXXXXXXXXX.....", "..XXXXXX.XXXXXXXXXX.", "..XXXXXXXXXXXXXXXXX.", "..XXXXXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".....XXXXXXXXXXXXXX.", ".........XXXXXXXXXX.", ".........XXXXXXXXXX.", ".........XXXXXXXXXX.", "....................", "...................." }{"....................", "....................", "....................", "...XX...............", "...XX...............", "....................", "....................", "....................", "....................", "XXX.................", "XXX.................", "XXX.....XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "........XXXXXXXX....", "...................." }0
0
20
13
11
Returns: {"....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "..............XX....", "..............XX....", "....................", "...................." }