Frank is in a hurry to throw his laptop into river (don't ask why). Unfortunately for him, there are a lot of traffic jams in his city today. Using mystical means and connections, Frank has managed to obtain "Map of chromosomes". Basically a map that informs its holder how much time it will take to pass through each district of the city. The map also informs its holder of "basedness" of each area, but we will exclude that, since it has no use in this problem.
Each district is presented as a square, with one positive integer that describes how much time is required to pass through it and enter new district. All districts altogether form a rectangle, Frank begins his journey from the last (southern) row meaning he has an option to choose his starting (last row) district. After that, he can move into north-west (up-left), north (up) or north-east (up-right) district from there. He will repeat this until he gets to any first (northern) row district, since all first row districts are adjacent to river.
Description of districts can be obtained with this link . In this file, the description goes as following: Firstly, there are two numbers: - number of row, - number of columns. After that, a matrix is given, where elements for are "northern row districts" and elements for are "southern row districts".
Assuming that we have found the "cheapest" (in means of time) route, answer for this solution is the following number:
- cost of the cheapest route or "time" taken to reach river.
- starting district's position. Frank's starting district is of form: , where is a number from set
- Frank's movement from district row to district row . This value is from set and they stand for:
- movement to north-east (up-left), - movement to north (up), - movement to north-west (up-right).
Example:
Southern row consists of numbers:
"The cheapest" route is depicted with boxed numbers.
Starting district is , so value .
since Frank moved from district to (up-left or north-east)
(just up or north)
So the final answer for this example is:
Notes:
The "cheapest" route is unique
Link , once more
Link for questions: here
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
This problem is primed for a dynamic programming solution. Let's define F ( i , j ) to be the shortest time to reach the district in the i th row and j th column. Then we can see that F ( N , j ) = d N , j ∀ j = 1 , 2 , … , M F ( i , 0 ) = F ( i , M + 1 ) = ∞ ∀ i = 1 , 2 , … , N F ( i , j ) = min { F ( i + 1 , j − 1 ) , F ( i + 1 , j ) , F ( i + 1 , j + 1 ) } + d i , j ∀ i = 1 , 2 , … , N − 1 , j = 1 , 2 , … , M [Note that the second line is just to make the computation in third line easier, and in the context of the problem ∞ can be any finite number larger than i , j ∑ ( d i , j ) ]
Using this, the cost of the cheapest route is just C = j min { F ( 1 , j ) }
Note that I haven't cared to keep track of the m i or the S . The reason for this is due to S + i = 1 ∑ N − 1 m i = S + i = 1 ∑ N − 1 ( m i − 2 ) + 2 ( N − 1 ) = E + 2 ( N − 1 ) where E = argmin j { F ( 1 , j ) } denotes the ending district's position. Therefore, the complete answer is C + S + i = 1 ∑ N − 1 m i = C + E + 2 ( N − 1 ) = j min { F ( 1 , j ) } + argmin j { F ( 1 , j ) } + 2 ( N − 1 )
When you run the dynamic programming scheme through your favorite language, we find C = 9 9 5 8 , E = 4 6 5 , 2 ( N − 1 ) = 9 9 8 C + E + 2 ( N − 1 ) = 1 1 4 2 1