#C4108. Maze Shortest Path

    ID: 47610 Type: Default 1000ms 256MiB

Maze Shortest Path

Maze Shortest Path

In this problem, you are given a maze represented by a 2D grid of size (n \times m). Each cell in the grid is either open (represented by 0) or blocked (represented by 1). Your task is to determine the shortest path from the start point at the top-left corner (cell ((0,0))) to the end point at the bottom-right corner (cell ((n-1, m-1))). You can move in four directions: up, down, left, and right. The length of the path is defined as the number of steps taken. If there is no valid path, print -1. Note that if the start or the end cell is blocked, there is no valid path.

inputFormat

The input is read from standard input (stdin). The first line contains two integers (n) and (m) separated by a space, representing the number of rows and columns of the maze respectively. This is followed by (n) lines, each containing (m) space-separated integers (0 or 1) representing the maze grid.

outputFormat

Output a single integer to standard output (stdout) which is the length of the shortest path from the start cell to the end cell. If no path exists, output -1.## sample

3 3
0 1 0
0 1 0
0 0 0
4