#P10257. Assembling the Puzzle
Assembling the Puzzle
Assembling the Puzzle
Little Maja has received a peculiar jigsaw puzzle on a sunny day. The puzzle consists of ( n ) rectangular pieces. Each piece is uniformly colored and has two numbers ( u ) and ( d ) written on its back. Maja discovered that these numbers indicate how the next piece should be attached to the current one. Specifically:
- If \( u = 0 \), the next piece is attached above the current piece. Its bottom‐left corner is aligned with the \( d \)th column (counted from the left) of the current piece’s top edge.
- If \( u = 1 \), the next piece is attached to the right of the current piece. Its bottom‐left corner is aligned with the \( d \)th row (counted from the bottom) of the current piece’s right edge.
The first piece is placed with its bottom‐left corner at the origin. Given the information for all pieces in assembly order (the parameters \( u \) and \( d \) for the first \( n-1 \) pieces determine how to attach the next piece; for the last piece, these values are given but unused), your task is to simulate the assembly and output a rectangular matrix that exactly contains the final puzzle. In the output grid, cells covered by a puzzle piece must display that piece's color, and cells not covered by any piece must contain a dot ('.').
inputFormat
The first line contains an integer ( n ) (the number of puzzle pieces). Each of the following ( n ) lines contains five tokens: two integers ( h ) and ( w ) (the height and width of the piece), a character (its color), and two integers ( u ) and ( d ). For the first ( n-1 ) pieces, ( u ) and ( d ) determine the attachment of the next piece as described above. The values ( u ) and ( d ) for the last piece are provided but are not used.
outputFormat
Output the final assembled puzzle as a rectangular character matrix. The matrix must exactly cover the minimal area that contains all the puzzle pieces. Print the top row first; each cell in the matrix should show the color of the puzzle piece covering it or a dot ('.') if no piece covers that cell.
sample
2
3 4 a 0 2
2 3 b 0 1
.bbb
.bbb
aaaa
aaaa
aaaa
</p>