#K88117. Escape the Maze
Escape the Maze
Escape the Maze
You are given a maze represented as a grid with n rows and m columns. Each cell in the grid is either an open cell denoted by a dot ('.') or an obstacle denoted by a hash ('#'). Your task is to determine the shortest path from the top-left cell (starting point) to the bottom-right cell (exit) by moving only up, down, left, or right. The length of the path is measured by the number of steps taken, including both the start and the exit cells.
In mathematical terms, if the number of steps in the shortest path is denoted by (d), you are required to compute (d). If either the start or the exit is blocked, or no valid path exists, output (-1).
Example of a maze with no valid path:
(\begin{array}{cccc} . & . & . & # \ # & # & # & # \ . & . & . & . \end{array})
In this case, the correct output is (-1).
inputFormat
The first line contains two integers (n) and (m) representing the number of rows and columns in the maze, respectively. This is followed by (n) lines, each containing a string of (m) characters (each character is either '.' or '#') representing the maze.
outputFormat
Output a single integer which is the minimum number of steps required to reach the bottom-right corner from the top-left corner. If no such path exists, output (-1). The answer should be printed to stdout.## sample
5 7
...#...
.#.#.#.
.#...#.
####.#.
.......
11