PrettyPrintASpiral
TCO09 Qual 3 · 2009-02-24 · by misof
Problem Statement
Consider an infinite square grid. Each square in the grid can be represented by a pair of integer coordinates that specify its row and column.
We will fill the entire grid with a spiral of positive integers. We will start by writing the number 1 into the square in row 0, column 0. Then we will write the number 2 into the square in row 0, column 1. From there, the spiral will continue in a counter-clockwise direction. The next few numbers are placed as shown in the scheme below:
columns
-3 -2 -1 0 1 2 3
--------------------
-3 | 37 36 35 34 33 32 31
-2 | 38 17 16 15 14 13 30
-1 | 39 18 5 4 3 12 29
rows 0 | 40 19 6 1 2 11 28
1 | 41 20 7 8 9 10 27
2 | 42 21 22 23 24 25 26
3 | 43 44 45 46 .. .. ..
Your task will be to return a
More precisely, you will be given four
The returned
- The
String[] contains one element for each row of the displayed rectangle. The elements are ordered by the row coordinate they describe. - Each element is a concatenation of space-separated tokens, each describing a cell in the corresponding row. The tokens are ordered by the column coordinates of cells they describe.
- All tokens in the entire output have the same length.
- The length of a token is minimal, i.e., equal to the number of digits in the largest number displayed.
- Tokens that contain numbers with fewer digits are padded from the left using spaces.
Notes
- The constraints guarantee that the returned String[] will have at most 50 elements, and the length of each element will be at most 50.
Constraints
- row1 will be between -5,000 and 5,000, inclusive.
- col1 will be between -5,000 and 5,000, inclusive.
- row2 will be between -5,000 and 5,000, inclusive.
- col2 will be between -5,000 and 5,000, inclusive.
- row2-row1 will be between 0 and 49, inclusive.
- col2-col1 will be between 0 and 4, inclusive.
0
0
0
0
Returns: {"1" }
The spiral starts at coordinates (0,0).
-1
-2
-1
1
Returns: {"18 5 4 3" }
Note that the single-digit values are now padded with spaces.
-2
2
0
3
Returns: {"13 30", "12 29", "11 28" }
-2
0
1
2
Returns: {"15 14 13", " 4 3 12", " 1 2 11", " 8 9 10" }
-1
0
1
2
Returns: {" 4 3 12", " 1 2 11", " 8 9 10" }
Submissions are judged against all 154 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PrettyPrintASpiral with a public method vector<string> getWindow(int row1, int col1, int row2, int col2) · 154 test cases · 2 s / 256 MB per case