#C10291. Escape the Grid
Escape the Grid
Escape the Grid
You are given a grid of size (n \times m) where each cell is either free (represented by a dot '.') or blocked (represented by a hash '#'). Your task is to determine the minimum number of moves needed to travel from the top-left corner of the grid (cell (1,1)) to the bottom-right corner (cell (n,m)). You can move up, down, left, or right. If there is no valid path, output -1.
The input begins with two integers (n) and (m) indicating the number of rows and columns, respectively, followed by (n) lines each containing a string of length (m) that represents the grid. The output should be a single integer representing the minimum number of moves or -1 if the destination cannot be reached.
inputFormat
The first line of the input contains two space-separated integers (n) and (m). The following (n) lines each contain a string of length (m) where '.' indicates an empty cell and '#' indicates an obstacle.
outputFormat
Output a single integer: the minimum number of moves required to reach the bottom-right corner from the top-left corner. If there is no valid path, output -1.## sample
5 5
.....
.###.
.#.#.
.#.#.
.....
8