#K35097. Maze Path Finder

    ID: 25455 Type: Default 1000ms 256MiB

Maze Path Finder

Maze Path Finder

You are given an N x M maze represented as a grid of characters. Each cell in the maze is either an empty cell denoted by . or a wall denoted by #. Starting from the top-left corner (cell (1,1)), your task is to determine whether there exists a path to the bottom-right corner (cell (N,M)).

You may move in four directions: up, down, left, and right. Diagonal moves are not allowed, and you cannot pass through wall cells. If a path exists, output YES; otherwise, output NO.

Note: The grid is 1-indexed for description, but input is given in a 0-indexed format starting with the first row. Use a graph traversal algorithm such as BFS to solve the problem effectively.

inputFormat

The first line of input contains two integers, N and M, separated by a space, representing the number of rows and columns, respectively. This is followed by N lines, each containing a string of length M consisting of characters . and #.

outputFormat

Print a single line to standard output containing YES if there exists a path from the top-left corner to the bottom-right corner of the maze; otherwise, print NO.## sample

4 4
....
...#
#...
....
YES