#K56402. Shortest Path in a Grid
Shortest Path in a Grid
Shortest Path in a Grid
You are given a rectangular grid of size ( R \times C ) where each cell is either empty (denoted by 0) or contains an obstacle (denoted by 1). You start from the top-left cell ( (0,0) ) and your goal is to reach the bottom-right cell ( (R-1, C-1) ). You are only allowed to move right or down at each step. Your task is to determine the length of the shortest path that avoids all obstacles. If there is no valid path, return (-1).
Note: The path length is defined as the number of moves from the starting cell to the destination.
inputFormat
The first line contains two integers ( R ) and ( C ), representing the number of rows and columns, respectively. This is followed by ( R ) lines, each containing ( C ) space-separated integers (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 no such path exists, output (-1).## sample
5 5
0 0 0 0 1
0 1 0 0 0
0 0 1 1 0
1 0 0 0 0
0 0 0 1 0
8