Editorial for Baltic OI '11 P4 - Treasures and Vikings
Submitting an official solution before solving the problem yourself is a bannable offence.
Solution
With a bread first search (BFS), it is calculated how many rounds the Viking ship uses to get to any sea field. From every field you get to in the BFS you will go horizontally and vertically (from this field) and note that the Viking can see these fields at the time they get to the first field. Since there are fields, and there are at most fields that are horizontal or vertical to a given field the running time is .
Then make a BFS again, this time starting from the Y-position, so that you will find all fields you can go to before the Viking can see them. Then just check if you can reach the treasure. This takes times since there are fields, so the total running time is .
Solution
The solution is based on the same idea. Initially: Find all "horizontal sea pieces", i.e. every sequence of horizontally aligned sea fields which have island fields (or the end of the map) to the left and to the right. When you do the BFS then every time you go horizontally to note that the Viking can see the fields, just store that the Viking can see this piece. Do the same vertically and you only check each field twice giving a running time of .
Comments