Вы находитесь на странице: 1из 2

,

Is it reachable?
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes

SETUP: You are given a grid of size N ∗ M i.e with N rows and M columns. From any cell in the
grid, you can move in four directions: Up, Down, Left, Right. However, you cannot go outside the
boundary of the grid. For example, if you are in the topmost row you cannot go Up. Now, some cells are
marked as ′ X ′ and are prohibited. That means, you cannot enter these cells. Other cells in which you are
free to go are marked as ′ .′

PROBLEM: You are given Q queries. In each query you have will have two integer co-ordinates: x1 , y1
and x2 , y2 . Let us say the S represents the point (x1 , y1 ) and T represents the point (x2 , y2 ). You have
to answer whether there exists a path from S to T in the given grid. That means, does their exist at least
one set of directions that will take you from S to T such that you do not enter any prohibited cell?

Input
The first line contains two integers N and M .
The next N lines contain M characters each. ’.’ represents a free cell and ’X’ represents a prohibited cell.
The next line contains an integer Q which denotes the numbers of queries.
Each of the next Q lines contain 4 integers: x1 , y1 , x2 , y2 .

Output
For every query, output "Yes"(without the quotes) if there exists a path from S to T else output "No".

Constraints:
1 ≤ N, M ≤ 103
1 ≤ Q ≤ 105
1 ≤ x 1 , x2 ≤ N
1 ≤ y1 , y 2 ≤ M

Example
standard input standard output
5 5 NO
...X. YES
..X.. NO
...XX YES
XXXXX YES
..... NO
6
1 1 1 5
1 1 3 2
1 2 2 4
5 3 5 5
5 1 5 2
5 5 2 5

Note
Note that the grid is 1-indexed.

Page 1 of 2
,

Also, you are allowed to use ArrayLists.

Page 2 of 2

Вам также может понравиться