#K70437. Path Finding in a Grid

    ID: 33308 Type: Default 1000ms 256MiB

Path Finding in a Grid

Path Finding in a Grid

You are given an ( n \times m ) grid where each cell is either an open cell denoted by '.' or contains a tree represented by '#'. Your task is to determine whether there exists a valid path from the top-left corner (cell ((1,1))) to the bottom-right corner (cell ((n,m))). You can move only in four directions: up, down, left, or right, and you may only traverse open cells ('.').

If the starting cell or the destination cell contains a tree, then the answer is "NO". Otherwise, determine if a continuous path exists that avoids all trees. This problem can be solved using breadth-first search (BFS) or any other standard graph traversal algorithm.

inputFormat

The first line contains two integers ( n ) and ( m ), representing the number of rows and columns respectively. The following ( n ) lines each consist of a string of length ( m ) composed of the characters '.' and '#' representing the grid.

outputFormat

Output a single line: "YES" if there is a valid path from the top left corner to the bottom right corner, otherwise output "NO".## sample

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