#C7667. Pathfinding in a Grid
Pathfinding in a Grid
Pathfinding in a Grid
You are given a grid with ( n ) rows and ( m ) columns. Each cell is either free (denoted by a dot '.') or blocked (denoted by a '#' symbol). You are also given the coordinates of a starting cell ( (r_1, c_1) ) and a destination cell ( (r_2, c_2) ). Your task is to determine if there exists a path from the starting cell to the destination cell, moving only up, down, left, or right, and without stepping into blocked cells.
The movement is allowed only within the bounds of the grid. Use the Breadth-First Search (BFS) algorithm to efficiently determine if such a path exists.
The coordinates provided are one-indexed. Please output ( \texttt{YES} ) if the path exists, otherwise output ( \texttt{NO} ).
inputFormat
The first line contains two integers ( n ) and ( m ) representing the number of rows and columns of the grid.
The next ( n ) lines each contain a string of length ( m ) consisting of characters '.' (free cell) or '#' (obstacle).
The last line contains four integers: ( r_1, c_1, r_2, c_2 ) representing the starting cell (( r_1, c_1 )) and the destination cell (( r_2, c_2 )). All indices are 1-indexed.
outputFormat
Output ( \texttt{YES} ) if there is a valid path from the starting cell to the destination cell; otherwise, output ( \texttt{NO} ).## sample
4 4
....
..#.
.#..
....
1 1 4 4
YES