#C11430. Shortest Path in a Grid
Shortest Path in a Grid
Shortest Path in a Grid
You are given a grid represented by a 2D matrix consisting of 0's and 1's. A cell with 0 indicates a free space and a cell with 1 represents an obstacle.
Your task is to determine the minimum number of moves required to travel from the top-left corner (0,0) to the bottom-right corner (n-1,m-1). You can move in four directions: up, down, left, and right. If the bottom-right cell is unreachable, output -1.
The distance is defined as the number of moves taken, and can be represented by the formula $$\text{distance} = \min_{\text{path}} (\text{steps})$$.
inputFormat
The first line contains two integers n and m, representing the number of rows and columns of the grid respectively. This is followed by n lines, each containing m integers (0 or 1) separated by spaces, representing the grid.
outputFormat
Output a single integer, which is the minimum number of moves required to reach the bottom-right corner from the top-left corner, or -1 if it is unreachable.
## sample3 3
0 0 1
0 1 0
0 0 0
4