#K61592. Maze Path Validation

    ID: 31343 Type: Default 1000ms 256MiB

Maze Path Validation

Maze Path Validation

You are given a maze represented by a grid of n rows and m columns. Each cell in the maze is either open (denoted by '.') or blocked (denoted by '#').

Your task is to determine if there exists a valid path from the starting cell to the ending cell. You are allowed to move up, down, left, or right (no diagonal moves).

Note: The starting and ending cells must be open. If either is blocked, the answer is NO.

Mathematical formulation:

Find a sequence of moves such that for every move from cell \((x,y)\) to \((x',y')\), \[ 0 \leq x,x' < n,\quad 0 \leq y,y' < m,\quad maze[x][y] = maze[x'][y'] = '.' \] and the adjacent moves satisfy one of the following moves in \(\{(-1,0), (1,0), (0,-1), (0,1)\}\). The path starts at \((s_x,s_y)\) and ends at \((e_x,e_y)\).

inputFormat

The first line contains two integers n and m (the number of rows and columns of the maze).

The following n lines each contain a string of length m, representing the rows of the maze.

The next line contains two integers: the starting cell coordinates \(s_x\) and \(s_y\) (0-indexed).

The final line contains two integers: the ending cell coordinates \(e_x\) and \(e_y\) (0-indexed).

outputFormat

Output a single line: YES if there exists a valid path, otherwise NO.

## sample
5 5
.....
.#.#.
.#.#.
.#.#.
.....
0 0
4 4
YES