#K57777. Shortest Path in a Grid
Shortest Path in a Grid
Shortest Path in a Grid
You are given a grid of dimensions \(M \times N\) consisting of 0s and 1s. A 0 represents an open cell and a 1 represents an obstacle. You are also given a starting cell and a target cell.
Your task is to find the length of the shortest path from the starting cell to the target cell by moving up, down, left, or right. If no path exists, output \(-1\). Note that the path length is defined as the number of moves required to reach the target.
Input Example:
For a grid of size 3x3, the input could be:
3 3 0 0 0 1 1 0 0 0 0 0 0 2 2
inputFormat
The first line of input contains two integers \(M\) and \(N\), representing the number of rows and columns respectively. The next \(M\) lines each contain \(N\) space-separated integers (either 0 or 1) representing the grid.
The following line contains two integers representing the starting cell coordinates (row and column), and the subsequent line contains two integers representing the target cell coordinates (row and column). All indices are 0-based.
outputFormat
Output a single integer representing the length of the shortest path from the starting cell to the target cell. If no such path exists, output \(-1\).
## sample3 3
0 0 0
1 1 0
0 0 0
0 0
2 2
4