#C6083. Minimum Steps in a Grid

    ID: 49804 Type: Default 1000ms 256MiB

Minimum Steps in a Grid

Minimum Steps in a Grid

Given an (R \times C) grid where each cell is either open (represented by a dot '.') or blocked (represented by a hash '#'), your task is to determine the minimum number of steps required to move from the top-left corner (cell ((0, 0))) to the bottom-right corner (cell ((R-1, C-1))). You may only move one cell at a time in one of the four cardinal directions: up, down, left, or right. It is guaranteed that both the start and the destination cells are open. Use an optimal search algorithm (such as Breadth-First Search) to solve the problem efficiently.

Note: The grid is provided as a sequence of strings, and input is read from standard input (stdin). Ensure that your solution prints the resulting minimum step count to standard output (stdout).

inputFormat

The input is provided from standard input as follows:

  1. The first line contains two integers (R) and (C) denoting the number of rows and columns in the grid, respectively.
  2. The following (R) lines each contain a string of length (C) representing the grid. Each character is either '.' (open cell) or '#' (blocked cell).

outputFormat

Output a single integer, which is the minimum number of steps required to reach the bottom-right cell from the top-left cell. If there is no valid path, output -1.## sample

3 3
...
.#.
...
4