Joy of Mobile Routing
Time limit1sMemory limit128 MB
Given grid building heights and antennas, find the shortest path from a start to a destination intersection where every visited intersection has line-of-sight to some antenna.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Geometry, Brute force
- Solved
- No attempts yet
Statement
A computer science professor who loves programming has traveled to Tehran to attend our regional programming contest as a spectator. He worked hard to come, and if he finds it fun enough he plans to hold a similar contest at his own university.
As one might expect, he has become lost in our large and crowded city. Worn out, he stops at an intersection when he suddenly remembers the phone number of a friend on the organizing committee and calls him at once. The friend understands the situation and offers to guide him by phone: at each intersection he tells the professor which direction to take, and once the professor reaches the next intersection there is another call for the next direction. This continues until the professor finally sees his friend waiting at the destination intersection.
Because of poor network coverage, not every intersection has mobile reception. The professor wants to reach the contest venue as quickly as possible along a route on which he can call his friend from every intersection where he still needs directions.
The city is a grid of rectangular blocks. Each block is a building 10 meters wide and 10 meters long, so the streets between the buildings meet at grid intersections. Standing at an intersection on the ground, the professor has reception when some antenna can see him: there is a straight line from his intersection to some point of the antenna that never passes through the interior of a building. The line may touch the surface of a building without losing the connection; only passing through the solid of a building blocks the signal. Each building has a given height, and each antenna is a vertical pole of a given height standing at an intersection.
Write a program that finds the length, in meters, of the shortest such route.
Input
The first line contains a single integer (), the number of independent test cases. The test cases follow.
The first line of a test case contains two integers and (), the number of rows and columns of building blocks. Each of the next lines contains non-negative integers (), the building heights; the first value is the leftmost building of the topmost row. The next two lines give the starting intersection and the destination intersection, each as a row coordinate followed by a column coordinate. The topmost-leftmost intersection is and the bottommost-rightmost intersection is .
The next line contains a single integer (), the number of antennas. Each of the next lines contains three integers , (, ) and (), meaning that an antenna of height stands at intersection .
Output
For each test case, print a single line with one integer: the shortest distance, in meters, that the professor must travel to reach the destination. If there is no valid route, print instead.