#K82192. Path Existence in Grid
Path Existence in Grid
Path Existence in Grid
You are given T test cases. For each test case, you are provided with an N×N grid. Each cell in the grid is either E
(which designates an empty space) or O
(which designates an obstacle). Your task is to determine if there exists a path from the top-left corner (cell (0,0)) to the bottom-right corner (cell (N-1, N-1)) by moving only to the right or down in each step.
Important rules:
- If the starting cell or the destination cell is an obstacle (
O
), then no valid path exists. - You may only move right or down from any cell.
For each test case, output YES
if a valid path exists, and NO
otherwise.
Note: The input will be provided via standard input (stdin
) and the output must be printed to standard output (stdout
).
inputFormat
The first line contains an integer T
denoting the number of test cases.
For each test case, the first line contains an integer N
representing the size of the grid. The following N
lines each contain N
space-separated characters, where each character is either E
or O
.
Example:
2 2 E E E O 3 E O E E E E O E E
outputFormat
For each test case, print a single line containing either YES
if there exists a valid path from the top-left to the bottom-right corner, or NO
if there does not exist such a path.
2
2
E E
E O
3
E O E
E E E
O E E
NO
YES
</p>