#K1306. Path Existence in a Grid
Path Existence in a Grid
Path Existence in a Grid
You are given a grid with N rows and M columns. Each cell in the grid is either empty (denoted by a .
) or blocked (denoted by a #
). The top row of the grid is considered row 0 and the bottom row is row N-1. Similarly, the left-most column is column 0 and the right-most is column M-1.
Your task is to determine whether there exists a path from the bottom-left cell (i.e., cell \( (N-1,0) \)) to the top-right cell (i.e., cell \( (0,M-1) \)). You can move one cell at a time in one of the four cardinal directions: up, down, left, or right, and you can only move through empty cells.
If the starting or ending cell is blocked, then the answer is immediately "No". Otherwise, output "Yes" if a valid path exists and "No" if it does not.
inputFormat
The input is read from standard input and contains:
- A line with two space-separated integers: N and M, representing the number of rows and columns respectively.
- N lines follow, each line contains a string of length M consisting only of the characters
.
and#
, representing the grid. The first line corresponds to the top row of the grid.
outputFormat
Output a single line to standard output: Yes
if there is a valid path from the bottom-left cell to the top-right cell, or No
otherwise.
3 3
...
.#.
...
Yes
</p>