Under construction

Still being built. Things may change

Robot Race

Time limit2sMemory limit1024 MB

Summary
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 n×mn \times m fields arranged in nn rows and mm 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 n×mn \times m and qq 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 nn, mm and qq: 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 nn lines contains mm characters describing the fields of the maze. The character . represents an empty field and the character # a field with an obstacle.

Then qq lines follow describing the pairs of coordinates. The ii-th of them contains four integers r1,ir_{1,i}, c1,ic_{1,i}, r2,ir_{2,i}, c2,ic_{2,i}: the row and the column of the initial field and the row and the column of the target field.

  • It holds 1≤n,m≤10001 \leq n, m \leq 1000 and 1≤q≤1061 \leq q \leq 10^6.
  • For all i∈{1,2,…,q}i \in \{1, 2, \ldots, q\} it holds 1≤r1,i,r2,i≤n1 \leq r_{1,i}, r_{2,i} \leq n and 1≤c1,i,c2,i≤m1 \leq c_{1,i}, c_{2,i} \leq m.
  • For all i∈{1,2,…,q}i \in \{1, 2, \ldots, q\} the fields in the maze with coordinates (r1,i,c1,i)(r_{1,i}, c_{1,i}) and (r2,i,c2,i)(r_{2,i}, c_{2,i}) are empty.
  • In 20% of the test cases q≤300q \leq 300.

Output

Output qq lines. The ii-th line should contain the word YES if the robot can get from the ii-th initial field to the ii-th target field, or the word NO otherwise.

Examples1

  1. Example 1

    Input
    3 4 5
    .#..
    .##.
    ....
    1 1 3 4
    1 3 3 4
    1 1 1 1
    1 1 2 4
    2 1 2 4
    
    Expected output
    YES
    YES
    YES
    NO
    NO