#C446. Grid Path Navigation
Grid Path Navigation
Grid Path Navigation
You are given a grid of size \( M \times N \) consisting of characters .
and #
. The character .
denotes an open cell, and #
denotes an obstacle. You need to determine whether there exists a path from the top-left cell (cell \( (0,0) \)) to the bottom-right cell (cell \( (M-1,N-1) \)). Movement is allowed in the four cardinal directions: up, down, left, and right. The path cannot pass through obstacles.
If either the starting cell or the ending cell is blocked, then the answer is immediately "NO". Otherwise, you may use any algorithm (such as Depth First Search (DFS)) to determine if a path exists.
Note: A valid path is one that stays within grid boundaries and does not enter any cell with a #
.
inputFormat
The input begins with an integer \( T \) (\( T \ge 1 \)) representing the number of test cases. For each test case, the first line contains two integers \( M \) and \( N \) (the number of rows and columns, respectively). The following \( M \) lines each contain a string of length \( N \) consisting only of the characters .
and #
.
outputFormat
For each test case, output a single line containing either "YES" if there exists a valid path from the top-left cell to the bottom-right cell, or "NO" otherwise.
## sample2
3 3
...
.#.
...
3 3
###
.#.
...
YES
NO
</p>