QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100

#17624. Backrooms

Estadísticas

The wizard Harry likes to bake. One day, he found a spell that could transport him to the "backrooms" (this pun only works in Swedish), and he was filled with joy. However, when he used the spell, he was not transported to a bakery, but to an infinitely large grid. Each cell in the grid is either free or blocked. In the grid, you can move up, down, left, or right to adjacent cells that are not blocked. After walking around for a while, he notices that the pattern of blocked cells is periodic. More precisely, there is an $R \times C$ pattern that repeats infinitely. See the figure below for exactly how this works.

img

Visualization of Sample 1. The base pattern is marked in red. $(0,0)$ is not blocked, while $(1,1)$ is. The coordinates of the first question are drawn at $(2,4)$ and $(4,3)$. Naturally, the grid continues infinitely beyond what is drawn.

To escape, he needs to reach a certain cell in the grid. However, his teleportation magic is a bit rusty, so he will ask you $Q$ questions of the form: "If I teleport to cell $(s_x, s_y)$, can I walk to cell $(g_x, g_y)$?"

Input

The first line contains two integers $R$ and $C$ ($1 \leq R,C \leq 1000$), the number of rows and columns of the grid.

The next $R$ lines each contain a string of length $C$, consisting of the characters . and #. These are the rows of the grid. The character # means the cell is blocked, and . means the cell is free.

After that follows a line with the integer $Q$ ($1 \leq Q \leq 10^5$), the number of questions you must answer.

The following $Q$ lines each contain four integers $s_x, s_y, g_x, g_y$ ($0 \leq s_x, s_y, g_x, g_y \leq 10^{12}$), the coordinates of a question. It is guaranteed that neither of the cells at these coordinates is blocked.

Output

For every question, print Yes if Harry can walk from cell $(s_x, s_y)$ to cell $(g_x, g_y)$, otherwise print No.

Scoring

Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.

Group Points Constraints
1 7 $R, C \leq 2$, $Q \leq 5$, $s_x, s_y, g_x, g_y \leq 500$
2 4 $R, C, Q \leq 5$, $s_x, s_y, g_x, g_y \leq 500$
3 8 $R, C, Q \leq 5$, $s_x, s_y, g_x, g_y \leq 10^5$
4 25 $R, C, Q \leq 5$
5 15 $R, C \leq 5$
6 21 $R, C \leq 25$
7 20 No additional constraints.

Sample Input 1

3 3
#.#
.#.
..#
5
2 4 4 3
0 0 2 1
0 0 0 0
900000002 900000004 900000004 900000003
2 1 1 2

Sample Output 1

Yes
No
Yes
Yes
No

Explanation of Sample

  • Question 1: Harry starts at $(2,4)$ and wants to move to $(4,3)$. To do this, he can move right, down and right.
  • Question 2: the start and goal are separated by walls, so the answer is No.
  • Question 3: Harry already starts at the goal, so he is done instantly.
  • Question 4: even if we are very far out, the grid keeps going. The answer turns out to be Yes.
  • Question 5: once again, the start and goal are separated by walls.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.