Editorial for COCI '20 Contest 1 #1 Patkice


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.

We save the input as a matrix mat. Let the starting island have coordinates (ox, oy), so mat[ox][oy] = o. We will try to send the ducks to each of the four directions, yielding four paths. For each of these paths, we count through how many cells it passes before reaching x, or say the distance is infinite if the path ends in . or o.

Implementation: we run the following algorithm four times, starting from (ox+1, oy), (ox-1, oy), (ox, oy+1) and (ox, oy-1). We maintain the current duck position as (px, py), and the length of the path in a variable d initialized to 0. If mat[px][py] is in <>^v, increment d and move (px, py) in the corresponding direction. (We can use dict or std::map, mapping the ordered pairs which describe the direction to characters, e.g. {'>': (0, 1)}). If mat[px][py] equals o or ., set d = \infty and stop processing this path. We are done if mat[px][py] equals x.

After the previous part, we have the distances d_N, d_E, d_W and d_S. If all d_i = \infty, there is no solution and we print :(.

Else, we print :) and the letter i for which d_i is minimal, breaking ties alphabetically.


Comments

There are no comments at the moment.