Looping Labyrinth
Time limit4sMemory limit512 MB
Decide for each of up to 200000 query cells whether a path through empty cells of an infinitely tiled grid reaches the origin.
- Level
Hard8 of 10
- Topics
- Union-find, Graph, BFS
- Solved
- No attempts yet
Problem
A labyrinth covers the whole plane. It is built from a pattern, a rectangular grid of rows and columns in which every cell is either empty or blocked. Copying the pattern in all four directions gives an infinite grid of cells.
Number the rows and the columns of the infinite grid with integers, negative numbers included. Row numbers grow downwards and column numbers grow to the right. The cell at coordinates is the origin. Every copy of the pattern fills an by rectangle whose upper left cell has a row number divisible by and a column number divisible by , and no copy is mirrored or rotated. The upper left cell of the pattern therefore lands on the origin, and its lower right cell lands on the cell with coordinates .
Escaping the labyrinth from a cell means reaching the origin through empty cells, moving one step up, down, left or right at a time.
You are given the pattern and a list of starting cells. For each starting cell, decide whether escaping is possible.
Input
The first line has two integers and (), the number of rows and the number of columns of the pattern. Each of the next lines has a string of exactly characters describing one row of the pattern. The character # marks a blocked cell and . marks an empty cell.
The next line has an integer (), the number of starting cells. The -th of the next lines has two integers and (), the row and the column of the -th starting cell.
The origin and every starting cell are empty.
Output
Print lines. Line holds yes if the labyrinth can be escaped from the -th starting cell, and no otherwise.
Hint
The picture shows the labyrinth of the first example. The shaded rectangle is the copy of the pattern that starts at the origin, and the cell marked x is the origin.
