#C3979. Path Existence in a Grid

    ID: 47465 Type: Default 1000ms 256MiB

Path Existence in a Grid

Path Existence in a Grid

You are given a grid of size n×mn \times m consisting of cells that are either land (denoted by 'L'), water ('W'), or rock ('R'). Your task is to determine whether there exists a path from the top-left corner (0,0)(0,0) to the bottom-right corner (n1,m1)(n-1, m-1), moving only through cells marked as 'L'. A move is allowed in one of the four cardinal directions (up, down, left, right). Note that a valid path can only traverse through cells where the value is 'L'. If either the starting cell or the ending cell is not 'L', the answer is "No".

More formally, let the grid be represented as GG with dimensions n×mn \times m. You need to check whether there exists a sequence of indices {(xi,yi)}i=0k\{(x_i, y_i)\}_{i=0}^k such that:

[ \begin{aligned} &(x_0, y_0) = (0,0), \quad (x_k, y_k) = (n-1, m-1), \ &\text{For each } i,; G(x_i, y_i) = 'L', \ &\text{For each } i,; |x_{i+1} - x_i| + |y_{i+1} - y_i| = 1. \end{aligned} ]

If such a sequence exists, output "Yes", otherwise output "No".

inputFormat

The input is read from standard input. The first line contains an integer TT, the number of test cases. Each test case starts with a line containing two integers nn and mm, the dimensions of the grid. This is followed by nn lines, each containing a string of length mm representing the grid row. Each character in the string is either 'L', 'W', or 'R'.

outputFormat

For each test case, print a single line containing either "Yes" or "No" to indicate whether there exists a valid path from the top-left cell to the bottom-right cell of the grid.## sample

4
3 3
LRL
LWR
LLL
3 3
LRL
RRR
LLL
3 3
LRL
LRR
LLL
3 3
WLL
LLL
LLW
Yes

No Yes No

</p>