#K66402. Taco Delivery: Minimum Movements in a Grid

    ID: 32412 Type: Default 1000ms 256MiB

Taco Delivery: Minimum Movements in a Grid

Taco Delivery: Minimum Movements in a Grid

In this problem, you are given a 2D grid of dimensions \(n \times m\). Each cell in the grid is either passable ('.') or blocked ('*'). A robot starts at the position \((sr, sc)\) and wants to reach the destination \((dr, dc)\). The robot can move in four directions: up, down, left, or right.

Your task is to determine the minimum number of movements required for the robot to reach its destination. If the destination cannot be reached, print \(-1\).

Note: The positions provided in the input are 1-indexed, but your implementation may convert them to 0-indexed if needed.

The movement cost for every valid step is 1.

inputFormat

The input is read from standard input (stdin) and has the following format:

 n m
 sr sc
 dr dc
 row1
 row2
 ...
 row_n

Where:

  • \(n\) and \(m\) are the number of rows and columns in the grid respectively.
  • \(sr, sc\) is the starting cell.
  • \(dr, dc\) is the destination cell.
  • Each of the next \(n\) lines contains a string of length \(m\) representing the grid. A dot ('.') represents an open cell and an asterisk ('*') represents an obstacle.

outputFormat

Output the minimum number of movements taken by the robot to reach the destination. If the destination cannot be reached, output \(-1\).

The result should be written to standard output (stdout).

## sample
5 5
1 1
5 5
.....
.*...
.....
...*.
.....
8