
The cities of the future will be laid out in perfect grids. Nobody will drive or take the bus. Transit problems will have been solved once and for all by placing teleportation devices (TDs) at intersections. Pedestrians entering an intersection containing a TD will have the option of continuing through the intersection as normal or using an app on their phone to trigger the TD and instantly teleport themselves to a new intersection. The TDs will be linked in pairs as shown below. Either TD in the pair can be used to instantly teleport a pedestrian to the other TD.
Intersections are identified using a pair of integers representing the horizontal street number (numbered North to South starting at
Suppose a future pedestrian wants to go from the point marked
The input will contain
Your job is to find the shortest path between the pedestrian's start and finish locations and report the number of city blocks the pedestrian will have to walk. The path can make use of any number of teleportation devices.
Note that the data set below contains only
Sample Input
8 9
7 0
0 8
6
1 1 6 6
4 1 4 7
6 1 2 5
2 3 6 2
1 8 7 5
2 4 1 7
5 4
4 0
0 3
2
2 1 0 3
4 1 2 2
1000 1000
5 2
10 970
14
0 3 4 5
1 1 13 13
1 2 2 3
7 7 9 9
1 70 70 1
9 1 1 12
2 2 3 3
3 7 2 900
3 900 5 95
3 90 5 100
4 5 5 4
6 7 8 9
9 9 8 8
9 9 5 3
Sample Output
5
2
83
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org
Comments
The last two lines of the sample input both contain a teleporter at
. Is the previous one removed in this case, or are both stored at that coordinate?
Edit: Test cases are weak, so it doesn't matter whether or store all of them or take the new one.