Excursions
Time limit3sMemory limit1024 MB
Given a grid of nice and ugly cells, answer Q queries with the shortest 4-directional path between two cells that avoids ugly cells, or -1 if none exists.
Problem
Kasandra finally opened her first travel agency. Her trips are limited to Croatia for now, which we can picture as a table of size . Some cells are nice and marked with ., while the others are ugly and marked with #. Tourists can move in only four basic directions: up, down, left, and right.
Kasandra currently has planned trips. For each one, she wants the minimum number of steps from the starting point to the final destination without passing through any ugly cell. If the destination cannot be reached from the starting point, print .
Input
The first line contains the natural numbers (), (), and ().
The next lines describe the table as explained in the problem.
The next lines each contain four numbers , , , (, ). The pair is the row and column of the starting point, and is the row and column of the destination.
Output
Print lines. The -th line contains the distance between the starting point and the destination of the -th trip.