Editorial for COCI '07 Contest 6 #5 Princeza


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.

Suppose we store in one linked list the points sorted by the sums of their coordinates (breaking ties by x-coordinate), and in another linked list we store the points sorted by the differences of their coordinates (again breaking ties by x-coordinate). Additionally, for each point we keep a pointer to its position in each list.

Imagine the frog is located in some point (x, y) and that the next direction to process is A. The next point in direction A is of the form (x+P, y+P) for a positive integer P. So, if it exists, the next point has the same difference of coordinates as the current point, and its x-coordinate is larger than the x-coordinate of the current point. Observe that the next point is exactly the successor of the current point in the second list. If the current point has no successor or the successor doesn't have the same difference of coordinates, the jump will not occur. If the jump occurs, delete the current point from both lists and move to the next point.

Similar reasoning can be applied to all four directions.


Comments

There are no comments at the moment.