#C830. Minimum Moves in Grid

    ID: 52267 Type: Default 1000ms 256MiB

Minimum Moves in Grid

Minimum Moves in Grid

Given a grid represented by characters 'C' (clear) and 'B' (blocked), your task is to determine the minimum number of moves required to travel from a starting position to a destination. Movement is allowed only in the four cardinal directions (up, down, left, right), and you may only move onto cells marked with 'C'. If the destination cannot be reached, output -1.

Input Format: The first line of input contains two integers \(n\) and \(m\), representing the number of rows and columns in the grid, respectively. The following \(n\) lines consist of a string of length \(m\) each, comprising characters 'C' or 'B'. Then, one line contains two integers representing the starting cell coordinates (row and column, 0-indexed), and another line contains two integers representing the destination cell coordinates (row and column, 0-indexed).

Output Format: Output a single integer which is the minimum number of moves required to reach the destination cell from the starting cell. If the destination is unreachable, output \(-1\).

inputFormat

The input is read from standard input (stdin) and consists of:

  1. A line containing two integers \(n\) and \(m\), the number of rows and columns of the grid.
  2. \(n\) subsequent lines, each with a string of length \(m\) comprised of characters 'C' (clear) or 'B' (blocked).
  3. A line with two integers representing the starting cell coordinates (row and column, 0-indexed).
  4. A final line with two integers representing the destination cell coordinates (row and column, 0-indexed).

outputFormat

The output is printed to standard output (stdout) and should be a single integer representing the minimum number of moves required to get from the starting cell to the destination cell. If it is impossible to reach the destination, output -1.

## sample
5 5
CCCCC
CBCCC
CBCCC
CCCCC
CCCCC
0 0
3 4
7