#K60852. Maze Minimum Steps

    ID: 31178 Type: Default 1000ms 256MiB

Maze Minimum Steps

Maze Minimum Steps

Tom is lost in a maze represented by a grid of dimensions m×nm \times n. Each cell of the grid is either an empty space (denoted by 0) or an obstacle (denoted by 1). Tom starts at the top-left corner (cell (0, 0)) and must reach the bottom-right corner (cell (m1,n1)(m-1, n-1)). He can move in four directions: up, down, left, or right, but he cannot move through cells containing obstacles. Your task is to determine the minimum number of steps required for Tom to reach his destination. If there is no valid path, output -1.

Note: The grid is provided via standard input. Each test case begins with two integers representing the dimensions of the grid followed by the grid rows.

inputFormat

Input is given from standard input (stdin). The first line contains two integers mm and nn, where mm is the number of rows and nn is the number of columns in the grid. This is followed by mm lines, each containing nn space-separated integers (either 0 or 1) representing the grid.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of steps required for Tom to reach from the top-left to the bottom-right of the maze. If it is not possible to reach the destination, output -1.## sample

5 5
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
8