#K56777. Shortest Path in a Grid with Obstacles
Shortest Path in a Grid with Obstacles
Shortest Path in a Grid with Obstacles
You are given a 2D grid of size \(n \times m\) where each cell is either 0
(navigable space) or 1
(obstacle). You are also given a starting cell and a target cell. Your task is to determine the length of the shortest path from the starting point to the target point. The drone (or agent) can move only in the four cardinal directions: up, down, left, or right. Diagonal moves are not allowed.
If the target cell is unreachable from the start cell, you should output \(-1\).
Note: The grid is 0-indexed.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains two integers (n) and (m) representing the number of rows and columns in the grid.
- The next (n) lines each contain (m) integers (either 0 or 1) separated by spaces, representing the grid.
- The following line contains two integers: the row and column indices of the starting cell.
- The last line contains two integers: the row and column indices of the target cell.
All indices are 0-indexed.
outputFormat
Output a single integer, which is the length of the shortest path from the start to the target cell. If the target is not reachable, output -1.## sample
4 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0
3 3
6