#K42517. Escape Maze

    ID: 27105 Type: Default 1000ms 256MiB

Escape Maze

Escape Maze

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

  • S: the starting cell
  • E: the exit cell
  • .: an empty space that can be traveled through
  • #: a wall that cannot be passed

Your task is to determine whether there is a path from the starting cell S to the exit cell E. You can move in four directions: up, down, left, and right. Consider the valid cell coordinates using the condition: \(0 \le i < m\) and \(0 \le j < n\).

For example, given the following maze:

5 5
S...#
..#.#
..#.#
.....
#...E
The output should be YES because a valid path exists from S to E.

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

The first line contains two integers m and n separated by a space, where 1 ≤ m, n ≤ 1000.

This is followed by m lines, each containing a string of length n representing a row of the maze. The maze will contain exactly one S and one E.

outputFormat

Print a single line to standard output (stdout) containing either "YES" if a path exists from S to E, or "NO" if no such path exists.## sample

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