#K37747. Maze Path Validation

    ID: 26045 Type: Default 1000ms 256MiB

Maze Path Validation

Maze Path Validation

In this problem, you are given a maze represented as a grid of characters. Each cell in the grid can be one of the following:

  • 'S': the starting cell
  • 'E': the exit cell
  • '#': a wall
  • ' ' (space): an empty cell

You need to determine if there exists a valid path from the starting cell 'S' to the exit cell 'E'. The movement is allowed in the four cardinal directions: up, down, left, and right. More formally, if we denote the maze as a matrix AA, you need to decide whether there exists a sequence of moves starting from some cell (iS,jS)(i_S,j_S) (where A[iS,jS]=SA[i_S,j_S]=S) and ending at some cell (iE,jE)(i_E,j_E) (where A[iE,jE]=EA[i_E,j_E]=E) such that every move goes to an adjacent cell (i.e. from (i,j)(i,j) to (i+1,j)(i+1,j), (i1,j)(i-1,j), (i,j+1)(i,j+1), or (i,j1)(i,j-1)) and no move goes into a wall (i.e. a cell where A[i,j]=#A[i,j]=\#).

The input and output are handled using standard input and output respectively.

inputFormat

The input is given from standard input. The first line contains an integer nn denoting the number of rows in the maze. The following nn lines each contain a string representing a row of the maze. All rows have equal length.

outputFormat

Output a single line to standard output: print 'Yes' if there exists a valid path from 'S' to 'E', and 'No' otherwise.## sample

3
S##
 # 
  E
Yes