#K46832. Maze Reachability

    ID: 28064 Type: Default 1000ms 256MiB

Maze Reachability

Maze Reachability

You are given a maze represented by a 2D grid of characters. Each cell in the grid is either a free cell denoted by . or a blocked cell denoted by #. The robot starts from the top-left corner (cell (0,0)). For each query, determine whether it is possible to reach the cell at coordinates \( (r, c) \) starting from (0,0) by moving up, down, left, or right through free cells only.

Note: The starting cell and the target cell must both be free (denoted by .). You are required to use standard input for reading the maze and queries, and standard output for printing the result. Output True if the target cell is reachable, and False otherwise.

The reachability condition can be expressed mathematically as follows. Let \( G \) be the grid and let a cell \( (i, j) \) be accessible if \( G[i][j] = . \). We say that \( (r, c) \) is reachable from \( (0, 0) \) if there exists a sequence of cells \( (0,0) = (i_0,j_0), (i_1,j_1), \dots, (i_k,j_k) = (r, c) \) such that for every \( t = 0,1,\dots,k-1 \), the cells \( (i_t,j_t) \) and \( (i_{t+1},j_{t+1}) \) are adjacent (i.e. \( |i_t-i_{t+1}| + |j_t-j_{t+1}| = 1 \)) and each \( G[i_t][j_t] = . \).

inputFormat

The first line contains two integers n and m representing the number of rows and columns of the grid, respectively. The next n lines each contain a string of m characters (each either . or #), describing the maze.

The following line contains a single integer q, the number of queries. Each of the next q lines contains two integers r and c indicating the row and column (0-indexed) of the target cell.

outputFormat

For each query, print a line with either True if the target cell is reachable from the start, or False if it is not.

## sample
4 4
...#
.#..
.#..
#...
1
3 3
True

</p>