Editorial for COCI '23 Contest 2 #2 Pingvin


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.

Prepared by: Marin Kišić and Martina Licul

To solve the first subtask, it was enough to check a few possible cases, which we will not describe here.

To solve the second subtask, it was enough to notice that the minimum number of wing flaps needed to get from a position with x-coordinate equal to x_s to a position with x-coordinate equal to x_f is equal to the absolute difference between x_f and x_s. The same holds for the y-coordinate and the z-coordinate. The total number of wing flaps is then equal to |x_f-x_s|+|y_f-y_s|+|z_f-z_s|, where |x| is the absolute value of the number x.

To solve the third subtask, we can omit the z-coordinate, because the penguin can only move to positions with z-coordinate equal to 1. If z_f \ne 1, then the answer is -1, because the penguin cannot reach the final position.

Omitting the z-coordinate, we have reduced the three-dimensional space to a two-dimensional plane. The algorithm that solves this task is breadth-first search. This algorithm starts from the initial position of the penguin and in each step it expands to all neighboring positions, checking whether it has already been to that position and whether it can be at that position. If both conditions are satisfied, then that position is added to the queue, and the number of wing flaps needed for that position is equal to the number of wing flaps needed for the position from which it came, increased by 1. After a position is processed, it is removed from the queue, and the algorithm continues with the processing of the next position in the queue. The algorithm stops when the final position is processed or when the queue is empty.

To solve the whole task, it is necessary to extend the algorithm from the previous subtask to a three-dimensional space. For more information about the breadth-first search algorithm, see the link.


Comments

There are no comments at the moment.