#C2657. Grid Pathfinding
Grid Pathfinding
Grid Pathfinding
You are given a grid with n rows and m columns. Each cell in the grid is either free (represented by '.') or blocked (represented by '#'). Your task is to determine whether there exists a path from the top-left corner (cell at position (0,0)) to the bottom-right corner (cell at position (n-1, m-1)) by moving only in the four cardinal directions (up, down, left, right).
Formally, given integers n and m and a grid G such that
find whether there exists a sequence of moves that starts at (0, 0)
and ends at (n-1, m-1)
, moving only to adjacent cells that are free.
inputFormat
The input is read from standard input (stdin
) and has the following format:
n m row1 row2 ... rown
Here, n
and m
are integers representing the number of rows and columns respectively, and each of the following n
lines represents a row of the grid.
outputFormat
Output a single line to standard output (stdout
) containing either Yes
if a path exists or No
otherwise.
5 5
.....
.###.
.#.#.
.#...
.....
Yes