#K59687. Shortest Path in a Grid
Shortest Path in a Grid
Shortest Path in a Grid
You are given a grid with dimensions \(M \times N\) consisting of characters '.' and '#'. The character '.' represents an open cell, while '#' represents an obstacle. Your task is to compute the minimum number of steps required to move from the starting cell \((Sx, Sy)\) to the destination cell \((Dx, Dy)\) using only the four cardinal directions: up, down, left, and right. If there is no possible path, output -1.
Note: The cell coordinates are provided in 1-indexed format.
Example: For a 5x5 grid with start cell (1,1) and destination cell (5,5), the grid is:
..... ..#.. ..#.. ..#.. .....
The minimum number of steps required is 8.
If needed, you may use LaTeX to represent formulas, for example: \(M \times N\).
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains two integers (M) and (N), representing the number of rows and columns of the grid, respectively.
- The second line contains four integers (Sx), (Sy), (Dx), and (Dy), which denote the starting cell and destination cell positions (1-indexed).
- The following (M) lines each contain a string of length (N) representing a row of the grid.
outputFormat
Output a single integer to standard output (stdout), which is the minimum number of steps required to move from the starting cell to the destination cell. If no such path exists, output -1.## sample
5 5
1 1 5 5
.....
..#..
..#..
..#..
.....
8
</p>