#C626. Path Existence in a Grid
Path Existence in a Grid
Path Existence in a Grid
You are given an \(n \times n\) grid represented by a list of strings. Each string consists of the characters '.'
(representing an empty cell) and '#'
(representing a blocked cell). Your task is to determine whether there exists a path from any cell in the leftmost column to any cell in the rightmost column by moving only up, down, left, or right through empty cells.
Note: The path can start at any cell in the leftmost column and end at any cell in the rightmost column.
Mathematically, given a grid \(G\) where \(G_{ij}\) is either '.'
or '#'
, determine if there exists a sequence of valid moves starting from some cell \(G_{i,0}\) (with \(G_{i,0} = '.'\)) to some cell \(G_{j,n-1}\) (with \(G_{j,n-1} = '.'\)).
inputFormat
The first line of input contains a single integer \(n\), denoting the size of the grid.
The following \(n\) lines each contain a string of length \(n\) consisting only of the characters '.'
and '#'
, representing the grid.
outputFormat
Output a single line with the string YES
if there exists a path from the leftmost column to the rightmost column through empty cells; otherwise, output NO
.
5
..##.
#..#.
.#..#
.#..#
.##..
YES
</p>