#C6792. Maze Pathfinder

    ID: 50591 Type: Default 1000ms 256MiB

Maze Pathfinder

Maze Pathfinder

You are given a maze represented as a grid of characters. Each cell of the maze is one of the following:

  • S: starting position
  • E: ending position
  • .: an open path
  • #: a wall

Your task is to determine whether there exists a path from the start cell S to the end cell E moving only up, down, left, or right through open cells. Formally, for a given maze of dimensions \( R \times C \), determine if there exists a sequence of moves such that:

\[ \text{start} = (i_0,j_0) \rightarrow (i_1,j_1) \rightarrow \cdots \rightarrow (i_k,j_k) = \text{end} \]

and for every step \( (i_{l}, j_{l}) \) to \( (i_{l+1}, j_{l+1}) \), the cell \( maze[i_{l+1}][j_{l+1}] \) is either \( '.' \) or \( 'E' \), with moves only allowed in the four cardinal directions.

inputFormat

The first line of input contains two integers \( R \) and \( C \) separated by a space, indicating the number of rows and columns in the maze.

This is followed by \( R \) lines, each containing a string of exactly \( C \) characters. The characters can be:

  • S for the start,
  • E for the end,
  • . for an open cell, or
  • # for a wall.

It is guaranteed that the input represents a valid maze.

outputFormat

Output a single line with True if there exists a path from S to E in the maze, and False otherwise.

## sample
5 5
S.##.
..##.
#....
##.#.
...#E
True