#K37217. Path Existence in a Grid

    ID: 25928 Type: Default 1000ms 256MiB

Path Existence in a Grid

Path Existence in a Grid

You are given a square grid of size ( n \times n ) populated with characters. Each cell of the grid is either a start point 'S', an end point 'E', an empty space '.', or an obstacle '#' . Your task is to determine whether there exists a path from 'S' to 'E'. A valid move is one step in any of the four cardinal directions (up, down, left, right) into a cell that is either an empty space ('.') or the end cell ('E'). The start cell 'S' is the starting point of the path. If a path exists, output 'Yes'; otherwise, output 'No'.

The problem can be formulated as follows:

Given an integer ( n ) and a grid ( G ) ( (n \times n) ) where ( G_{ij} ) is either 'S', 'E', '.', or '#' , determine if there exists a path ( P ) from the cell marked 'S' to the cell marked 'E' such that every consecutive pair of cells in ( P ) are adjacent (up, down, left, or right) and every cell in ( P ) (except for 'S') is either '.' or 'E'.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer ( n ) representing the size of the grid.
  • The next ( n ) lines each contain a string of exactly ( n ) characters. Each character is one of 'S', 'E', '.', or '#'.

It is guaranteed that there is exactly one 'S' and one 'E' in the grid.

outputFormat

Output to standard output (stdout) a single word: 'Yes' if there is a valid path from 'S' to 'E', otherwise 'No'.## sample

5
S...#
.#.#.
.#E#.
.#..#
....#
Yes