Editorial for COCI '23 Contest 2 #2 Pingvin
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 -coordinate equal to to a position with -coordinate equal to is equal to the absolute difference between and . The same holds for the -coordinate and the -coordinate. The total number of wing flaps is then equal to , where is the absolute value of the number .
To solve the third subtask, we can omit the -coordinate, because the penguin can only move to positions with -coordinate equal to . If , then the answer is -1
, because the penguin cannot reach the final position.
Omitting the -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 . 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