Canadian Computing Competition: 2005 Stage 2, Day 2, Problem 2
You are to find the length of the shortest path from the top to the bottom of a grid covering specified points along the way.
More precisely, you are given an
must be visited. Notice that one step is taken when dropping down between consecutive rows. Note that you can only move left, right and down (you cannot move up a level). On finishing the segment on row
Input Specification
The first line of input consists of an integer
Output Specification
The output is one integer, which is the length of the (shortest) path from
Sample Input
6
2 6
3 4
1 3
1 2
3 6
4 5
Sample Output
24
Explanation for Sample Output
Below is a pictorial representation of the input.
1 2 3 4 5 6
. _________________________________
________
_________________
_________
_________________________
_________ .
Notice that on the first row, we must traverse
On the second row, we must traverse
On the third row, we must traverse
On the fourth row, we move
On the fifth row, we move
On the sixth (and final) row, we move
In total, we have moved
Comments