#C3979. Path Existence in a Grid
Path Existence in a Grid
Path Existence in a Grid
You are given a grid of size 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 to the bottom-right corner , 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 with dimensions . You need to check whether there exists a sequence of indices 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 , the number of test cases. Each test case starts with a line containing two integers and , the dimensions of the grid. This is followed by lines, each containing a string of length 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>