Evil Problemsetters
Time limit10sMemory limit1024 MB
Given a grid with up to 42 black holes, find the shortest path from the entrance to the exit for each query, or -1 if it is unreachable.
- Level
Hard9 of 10
- Topics
- Shortest path, Graph, Implementation
- Solved
- No attempts yet
Problem
Some contestants decide to escape from this contest because of the challenging problems. To stop them, the EVIL problem setters built a labyrinth at the stadium's exit. The labyrinth is an grid with an entrance, an exit, and black holes. A contestant who accidentally steps into a black hole falls into it and can never escape from the contest.
Worse, the problem setters may change the coordinates of the entrance and the exit. You are a poor contestant who starts at the entrance and wants to reach the exit without stepping into any black hole. Each step moves you to one of the four adjacent cells. After each time the problem setters change the coordinates of the entrance and the exit, find the minimum number of steps needed to get from the entrance to the exit.
Input
The first line contains four integers , the number of rows, the number of columns, the number of black holes, and the number of queries.
The next lines describe the black holes. Each line contains two integers , the coordinates of a black hole. No two black holes share a position.
The last lines describe the queries. Each line contains four integers , where is the entrance and is the exit.
Output
For each query, print the minimum number of steps needed to get from the entrance to the exit, one per line. If the exit cannot be reached, print . If the entrance or the exit is at the same position as a black hole, the exit is considered unreachable.
Hint
The layout of the labyrinth and one possible shortest path for the first query of the first sample are shown below.


