#C11314. Warehouse Delivery Route
Warehouse Delivery Route
Warehouse Delivery Route
You are given an m x n grid representing a warehouse. Each cell in the grid contains either a 0 or a 1. A 0 indicates that the cell is free while a 1 indicates an obstacle. A robot starts from the top-left corner of the grid and its goal is to reach the bottom-right corner by moving only to the right or down. The task is to determine if there exists a valid path from the start to the goal. If such a path exists, output the length of the shortest path (where the starting cell is counted as a step); otherwise, output -1.
Note: The length of the path is defined as the number of cells visited including the start and the destination. The movement is only allowed in two directions: right (\(0,1\)) and down (\(1,0\)).
For example, consider the grid below:
0 0 1 0 0 1 1 0 0
The shortest path is of length 5: (0,0) \(\rightarrow\) (0,1) \(\rightarrow\) (1,1) \(\rightarrow\) (2,1) \(\rightarrow\) (2,2).
inputFormat
The first line contains two integers \(m\) and \(n\) separated by a space, representing the number of rows and columns of the grid respectively. This is followed by \(m\) lines, each containing \(n\) space-separated integers (each being either 0 or 1), representing the grid.
outputFormat
Output a single integer representing the length of the shortest path from the top-left corner to the bottom-right corner if one exists, otherwise output -1.
## sample3 3
0 0 1
0 0 1
1 0 0
5