#B4469. 回形座位 / seat
回形座位 / seat
Problem Description
The homeroom teacher is arranging seats for your class. Each classroom can be viewed as an seat matrix, where each seat is represented by or : represents a boy, and represents a girl.
Now, a student recorded the seat information of this classroom according to the following rules:
- Scanning method: Starting from the top-left corner (first row, first column) of the matrix, traverse the entire matrix in a spiral (rectangular) order—first go from left to right along the first row, then from top to bottom along the remaining part of the last column, then from right to left along the remaining part of the last row, and finally from bottom to top along the remaining part of the first column; after completing the outermost layer, continue traversing the inner matrix in the same way until all seats have been traversed.
- Recording rule: Use a series of numbers to alternately represent the consecutive number of boys and girls, where the first number always represents the number of boys (for example: means that in order, there are boys, then girls, then boy, then girls).
For example:
In a seat matrix with side length , the recording sequence of boys and girls is: . The final seating order and result are:

Now, given this recording sequence, please restore the original seat matrix.
The data guarantees: the sum of all numbers in the recording sequence equals , and the sequence strictly alternates to represent consecutive numbers of boys and girls.
Input Format
The first line contains two integers and , representing the length of the recording sequence, and the number of rows and columns of the matrix.
The second line contains positive integers separated by spaces, representing the recording sequence of boys and girls.
Output Format
Output lines, each containing integers ( or ) separated by spaces, representing the restored seat matrix.
3 3
3 3 3
0 0 0
0 0 1
0 1 1
Hint
[Sample Explanation]
-
Traversal order: $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,2)\rightarrow(3,1)\rightarrow(2,1)\rightarrow(2,2)$
-
Recording sequence analysis:
- First number : consecutive boys (corresponding to the first positions: , , );
- Second number : consecutive girls (corresponding to the next positions: , , );
- Third number : consecutive boys (corresponding to the last positions: , , ).
[Constraints]
For of the data, ;
For another of the data, ;
For another of the data, the recording sequence contains only boys.
For another of the data, .
For another of the data, and all numbers in the recording sequence are .
For of the data, , , and the sum of the recording sequence equals .
Translated by DeepSeek-V3