Editorial for COCI '23 Contest 5 #1 Zlagalica


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
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 20 puzzles with a height and width of 10), our completed puzzle cannot have more than 200 rows and 200 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 (x, y) 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 (a, b) be the point in the matrix where the bottom-left corner of the puzzle piece we just placed in the matrix is located, let r and s be the number of its rows and columns, and u and d be the numbers written on its back. If the next puzzle is added on top of the previous one (i.e., u = 0), (x, y) will be equal to (a-r, b+d-1). If the next puzzle is added to the right of the previous one (i.e., u = 1), (x, y) will be equal to (a-r+d, b+s).

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

There are no comments at the moment.