#K45352. Path Existence in a Grid Maze
Path Existence in a Grid Maze
Path Existence in a Grid Maze
You are given an \(N \times M\) grid maze where each cell is represented by one of the following characters:
S
for the starting pointE
for the ending point.
for an empty cellX
for an obstacle
Your task is to determine if there exists a path from the starting point \(S\) to the ending point \(E\). You are allowed to move up, down, left, or right and you can only traverse through cells that are not obstacles (i.e. not X
). The starting and ending cells are always considered accessible.
Solve this problem using any method (such as breadth-first search) that correctly determines whether such a path exists.
inputFormat
The first line of input contains two space-separated integers \(N\) and \(M\) representing the number of rows and columns in the grid, respectively. The following \(N\) lines each contain a string of length \(M\), representing a row of the maze.
It is guaranteed that there is exactly one \(S\) (starting point) and one \(E\) (ending point) in the grid.
outputFormat
Output a single line to standard output: print YES
if there exists a valid path from \(S\) to \(E\); otherwise, output NO
.
3 3
S..
.X.
..E
YES