#B4322. Square Partition Conversion
Square Partition Conversion
Square Partition Conversion
Keko has invented a new method to partition a square. Initially, there is a square which we call the round figure. In the first round (round ), the square is divided evenly into 4 smaller squares: top-left, top-right, bottom-left, and bottom-right. They are labeled as , , , and respectively.
For every subsequent round, each square from the previous round is divided into 4 parts following the same pattern. In round , for example, the four parts of the square originally labeled as will be labeled , , , and respectively (where the second letter represents the division within that square). In general, at round , each cell is identified by a string of length composed of the characters {A, B, C, D}.
The mapping from letter to position within a sub-square is defined as follows (each sub-square is a grid):
- : top-left cell (row offset 0, column offset 0)
- : top-right cell (row offset 0, column offset 1)
- : bottom-left cell (row offset 1, column offset 0)
- : bottom-right cell (row offset 1, column offset 1)
When the entire square is subdivided for round , the grid size becomes . Rows are numbered from top to bottom starting from , and columns are numbered from left to right starting from .
Your task is to convert between a cell's identifier (a string consisting of letters A, B, C, D) and its position (row and column numbers) in the grid.
The program should support two types of queries. Each test case begins with an integer indicating the query type:
-
If the query type is 1, then the next token is a string representing the cell's identifier. You must output the cell's position in the format "row column" (both 1-indexed).
-
If the query type is 2, then the next three integers are provided: , , and , where is the round number (i.e. the length of the identifier), and and (1-indexed) specify the cell's row and column in the grid. You must output the corresponding cell identifier.
For example, given the identifier \tt{AB} in query type 1, the answer would be "1 2". Conversely, for query type 2 with input "2 1 2", the expected identifier is \tt{AB}.
Note: It is guaranteed that the provided row and column numbers will be valid for the specified .
inputFormat
The first line contains an integer , the number of test cases. Each of the following test cases begins with an integer indicating the query type.
If the query type is 1, it is followed by a string (without spaces) representing the cell identifier.
If the query type is 2, it is followed by three integers: , , and , where (the number of rounds), (the row number), and (the column number) describe the position in a grid.
All tokens are separated by whitespace.
outputFormat
For each test case, output a single line:
- If the query type is 1, output two integers separated by a space: the row and column (both 1-indexed) corresponding to the given identifier.
- If the query type is 2, output the cell's identifier (a string consisting of exactly characters from the set {A, B, C, D}).
sample
3
1 AB
2 2 1 2
1 D
1 2
AB
2 2
</p>