#K86352. Amy's Grid Navigation
Amy's Grid Navigation
Amy's Grid Navigation
Amy is trapped in a grid, starting from the top-left corner and aiming to reach the bottom-right corner. The grid has n rows and m columns. Some cells are accessible (represented by .
) while others are blocked (represented by #
). Amy can only move either down or right. Determine whether she can reach the destination.
This problem can be formulated as follows: find if there exists a path from the starting cell (1, 1) to the destination cell (n, m) using only moves in the downward or rightward direction. In mathematical notation, if we denote the grid as \(G\), you must decide whether there exists a sequence of moves satisfying
\[ (1,1) \to (i_1, j_1) \to (i_2,j_2) \to \cdots \to (n,m), \quad \text{with each move } (i,j) \to (i+1,j) \text{ or } (i,j+1). \]
inputFormat
The input begins with an integer t indicating the number of test cases. For each test case, the first line contains two integers n and m which represent the number of rows and columns of the grid, respectively. This is followed by n lines, each consisting of a string of m characters; each character is either .
(an open cell) or #
(a blocked cell).
outputFormat
For each test case, output a single line containing YES
if Amy can reach the bottom-right corner of the grid, or NO
otherwise.
2
4 4
....
.##.
.#..
....
3 3
.#.
###
.#.
YES
NO
</p>