Robot Race
Time limit2sMemory limit1024 MB
For each of up to a million queries on an n by m grid of obstacles, decide whether a monotone path moving only right or down connects the two given empty cells.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Array, Brute force
- Solved
- No attempts yet
Problem
A robot race on a maze will be held in Byteland. The maze has the shape of a rectangle and is divided into fields arranged in rows and columns. Each field is either empty or contains an obstacle.
The contestants register their robots for the competition and come to the maze one after another. Each contestant gets random coordinates of the initial field and the target field. The robot is then placed in the initial field and must get to the target field using a sequence of steps.
To make the game more challenging, the rules specify that in each step the robot can only move one field right or one field down in the maze. Moving the robot in any other direction is not allowed.
The contestant whose robot gets fastest from the initial field to the target field wins the competition. If the robot does not get to the target field in the time limit, the contestant is disqualified.
The organizers of the competition realized that if a contestant gets bad coordinates, the robot will not be able to get to the target field using any sequence of moves. In that case they would like to give the contestant another pair of coordinates.
You are given a map of a maze of size and pairs of coordinates of the initial and target fields. Determine for each pair of coordinates whether it is possible to get from the initial field to the target field using a sequence of steps right or down.
Input
The first line contains three integers , and : the number of rows of the maze, the number of columns of the maze and the number of pairs of coordinates.
Each of the following lines contains characters describing the fields of the maze. The character . represents an empty field and the character # a field with an obstacle.
Then lines follow describing the pairs of coordinates. The -th of them contains four integers , , , : the row and the column of the initial field and the row and the column of the target field.
- It holds and .
- For all it holds and .
- For all the fields in the maze with coordinates and are empty.
- In 20% of the test cases .
Output
Output lines. The -th line should contain the word YES if the robot can get from the -th initial field to the -th target field, or the word NO otherwise.