#K43642. Minimum Moves in a Grid
Minimum Moves in a Grid
Minimum Moves in a Grid
You are given a grid of size n x m where each cell contains either 0 or 1. A value of 0 represents an open cell, and 1 indicates a blocked cell. Starting from the top-left corner (0-indexed) of the grid, your task is to determine the minimum number of moves required to reach the bottom-right corner. You are only allowed to move right or down at each step.
If there is no possible path from the starting point to the destination, output -1.
Note: The number of moves is defined as the number of transitions between cells. For a grid containing only one cell, the number of moves is 0.
The movement constraints can be expressed in LaTeX as follows:
$$\text{Allowed moves: }(i,j) \to (i, j+1) \quad \text{or} \quad (i,j) \to (i+1, j)$$
inputFormat
The first line of the input contains two space-separated integers n and m representing the number of rows and columns of the grid respectively. This is followed by n lines, each containing m space-separated integers (either 0 or 1) representing the grid.
Example:
3 3 0 0 1 1 0 1 1 0 0
outputFormat
Output a single integer representing the minimum number of moves required to reach the bottom-right corner of the grid from the top-left corner. If there is no valid path, output -1.
## sample3 3
0 0 1
1 0 1
1 0 0
4