#K79422. Minimum Moves in a Grid
Minimum Moves in a Grid
Minimum Moves in a Grid
You are given an \( n \times m \) grid game where each cell is either accessible (denoted by '.') or blocked (denoted by '#'). Your task is to determine the minimum number of moves required to travel from the top-left corner (position \( (0, 0) \)) to the bottom-right corner (position \( (n-1, m-1) \)). Movement is allowed only in the four cardinal directions: up, down, left, and right.
If the destination is unreachable, output -1
.
Note: The moves are counted as the number of steps taken, and you may assume that initially the starting position is always accessible.
inputFormat
The first line of input contains two integers \( n \) and \( m \) representing the number of rows and columns, respectively.
The next \( n \) lines each contain a string of length \( m \) representing the grid. Each character in the string is either '.' (accessible) or '#' (blocked).
Input is provided via standard input (stdin).
outputFormat
Output a single integer representing the minimum number of moves required to reach the bottom-right corner from the top-left corner. If the destination is unreachable, output -1
.
Output should be written to standard output (stdout).
## sample3 3
...
.#.
...
4