#C8498. Shortest Path in a Grid with Obstacles

    ID: 52486 Type: Default 1000ms 256MiB

Shortest Path in a Grid with Obstacles

Shortest Path in a Grid with Obstacles

You are given a grid of size \(m \times n\) where each cell is either 0 (representing a free space) or 1 (representing an obstacle). Your task is to determine the minimum number of moves required to travel from the top-left cell (0,0) to the bottom-right cell (m-1,n-1) if you can move in four directions (up, down, left, right). If the destination cannot be reached, output -1.

Note: The movement is allowed only to adjacent cells (not diagonally), and you cannot move onto cells that contain an obstacle.

Input/Output: The problem uses standard input and output. The first line of input contains two integers \(m\) and \(n\) separated by a space. Each of the next \(m\) lines contains \(n\) integers which are either 0 or 1. Output a single integer representing the minimum number of moves from the top-left corner to the bottom-right corner, or -1 if no such path exists.

inputFormat

The first line contains two integers \(m\) and \(n\) (the number of rows and columns). This is followed by \(m\) lines, each containing \(n\) space-separated integers (either 0 or 1) representing the grid.

outputFormat

Output a single integer: the minimum number of moves required to travel from the top-left cell to the bottom-right cell. If the destination cannot be reached, output -1.

## sample
3 3
0 0 1
1 0 0
0 0 0
4