#K58732. Maze Path Finder
Maze Path Finder
Maze Path Finder
You are given a maze represented as a grid of characters. Each cell is either open (represented by a .
) or blocked (represented by a #
). The maze has n rows and m columns. Your task is to determine whether there exists a path from the top-left corner (cell (1,1)) to the bottom-right corner (cell (n,m)) moving only in the four cardinal directions (up, down, left, right) and only through open cells.
Note: Movement is allowed only if the cell is open. Formally, let \( maze[i][j] \) denote the state of the cell in the \( i^{th} \) row and \( j^{th} \) column. You need to decide if there exists a sequence of moves starting at \( (1, 1) \) and ending at \( (n, m) \) such that for every move, the destination cell satisfies \( maze[i][j] = '.' \).
If a valid path exists, output YES
, otherwise output NO
.
inputFormat
The first line of input contains two space-separated integers \( n \) and \( m \) (the number of rows and columns in the maze). The next \( n \) lines each contain a string of length \( m \) consisting of characters .
and #
, representing the maze.
Example:
5 5 ..... .###. ..... .###. .....
outputFormat
Output a single line containing either YES
if there exists a path from the top-left cell to the bottom-right cell, or NO
otherwise.
5 5
.....
.###.
.....
.###.
.....
YES
</p>