#K10426. Maze Path Finder

    ID: 23244 Type: Default 1000ms 256MiB

Maze Path Finder

Maze Path Finder

You are given a maze represented as a grid with n rows and m columns. Each cell in the grid can be one of the following characters:

  • S denotes the starting point.
  • E denotes the ending point.
  • . denotes a free cell.
  • # denotes an obstacle.

Your task is to determine whether there exists a valid path from the starting cell S to the ending cell E by moving in the four cardinal directions (up, down, left, right) without passing through obstacles. The answer should be YES if such a path exists and NO otherwise.

Hint: A Breadth-First Search (BFS) algorithm can be used to solve this problem effectively.

inputFormat

The first line contains two integers n and m separated by a space, representing the number of rows and columns of the maze respectively.

The following n lines each contain a string of length m representing the maze row. Each character in the string is one of S, E, ., or #.

outputFormat

Output a single line containing YES if there exists a path from S to E in the maze, or NO otherwise.

## sample
5 5
S..#.
.#..#
.#..#
..###
...E.
YES

</p>