Editorial for COCI '23 Contest 5 #1 Zlagalica
Submitting an official solution before solving the problem yourself is a bannable offence.
When solving this task, the key is to notice that by connecting pieces, the entire puzzle can only expand
upwards and to the right. Since the constraints are small enough, we know that in the worst case scenario
(when we have puzzles with a height and width of
), our completed puzzle cannot have more than
rows and
columns. Because of this, and the fact that the puzzle can only "expand" upwards and
to the right, we will create a solution matrix with at least that many rows and columns, and we will start
assembling our puzzle from its bottom-left point.
Now, in the for loop, we follow the order of connecting given in the task. Each time after adding a puzzle
piece to the matrix, we calculate from which point in the matrix will we start building the next
piece (we always start building each piece from its bottom-left corner and then go upwards and to the
right). Let
be the point in the matrix where the bottom-left corner of the puzzle piece we just placed
in the matrix is located, let
and
be the number of its rows and columns, and
and
be the numbers
written on its back. If the next puzzle is added on top of the previous one (i.e.,
),
will be equal
to
. If the next puzzle is added to the right of the previous one (i.e.,
),
will
be equal to
.
To know the final height and width of the assembled puzzle, we keep track of the "highest" row and the "rightmost" column we have reached while assembling. Using them, the dimensions of the finished puzzle can be easily calculated, as well as the requested image printed.
Comments