The Dog, the Traitor, and the Cables
Time limit2sMemory limit1024 MB
Find the maximum number of cable segments a dog can bite on a shortest path from (1,1) to each queried cell in a grid with cables.
- Level
Hard9 of 10
- Topics
- Graph, BFS, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Now the traitor has a dog! The deck of a space ship is a grid of cells. Rows are numbered from 1 to from top to bottom, and columns are numbered from 1 to from left to right. Some pairs of side-adjacent cells are separated by a cable segment.
At the start, the dog is in cell , the top left cell. It chooses one player, and let the chosen player be in cell . The dog picks one of the shortest paths from its start to (in one move, the dog goes from a cell to a side-adjacent cell). Then the dog and the player take turns moving. The dog runs along the chosen path, and the player runs toward the dog from the other end of the same path. The dog moves first. The process continues until the dog and the player are in the same cell. Each time the dog crosses a cable segment, it bites through it.
Help the players find the maximum number of cable segments the dog can bite through.
Input
The first line contains three integers , , and , the size of the grid and the number of cable segments (; , ).
Each of the next lines describes one cable segment with four integers , , , and (; ). These numbers give the positions of two side-adjacent cells and , and the cable segment lies on the border between them. It is guaranteed that the two cells are side-adjacent.
The next line contains one integer , the number of player positions to answer for ().
Each of the next lines contains two integers and , the position of the player (, ).
Output
Print lines. The -th line contains the maximum number of cable segments the dog can bite through in the -th case.
Hint

(a) In the first input, the cables are placed as follows.

(b) In the second input, the cables are placed as follows.