Editorial for COCI '07 Regional #2 Nikola
Submitting an official solution before solving the problem yourself is a bannable offence.
We model the problem as a shortest path problem in a graph in which the state is an ordered pair , where is the square at which Nikola is located and is the length of the preceding jump.
From state Nikola can move to state moving backwards, or to state moving forwards, assuming these jumps don't move him out of the playing area.
Notice that the graph is acyclic (meaning that there is no way to visit a state twice while traversing the graph), because forward jumps increase the parameter. Because of this, the length of the shortest path can be calculated with dynamic programming.
Let the function represent the smallest cost for Nikola to get from square to square , if the preceding jump was of length .
Here's how to calculate the value of the function:
- For or , ;
- For , ;
- Otherwise,
The solution is then .
Comments