#K2051. City Exploration
City Exploration
City Exploration
You are given a grid representing a city, where some cells are passable and some are obstacles. The grid has n rows and m columns, and is given as a matrix of characters. Each cell in the grid is one of the following characters:
S
: the starting position of the touristE
: the destination.
: an open cell that can be traversed#
: an obstacle that cannot be traversed
The tourist can move in the four cardinal directions (up, down, left, right) and cannot move outside the grid or through obstacles. Your task is to determine whether the tourist can reach the destination from the starting position.
Note that if either the starting point or the destination is missing, the answer should be NO
. Also, if the starting point and the destination are the same cell, it is considered that the destination is reached, and the answer should be YES
.
The grid dimensions follow the formula \( n \times m \).
inputFormat
The first line of input contains two space-separated integers ( n ) and ( m ) representing the number of rows and columns of the grid, respectively. The next ( n ) lines each contain a string of length ( m ) representing a row of the grid.
outputFormat
Output a single line containing either "YES" if the tourist can reach the destination or "NO" if not.## sample
4 4
.S..
..#.
..#E
....
YES