#C5537. Minimum Moves in Grid

    ID: 49197 Type: Default 1000ms 256MiB

Minimum Moves in Grid

Minimum Moves in Grid

In a warehouse represented as an n × m grid, each cell is either free (.) or blocked (#). A robot is located at a starting position and needs to reach a destination position. The robot can move one cell at a time in the four cardinal directions: up, down, left, or right. The robot cannot move diagonally or pass through blocked cells.

Your task is to determine the minimum number of moves required to reach the destination. If no valid path exists, output -1.

Coordinates are represented using \( (x, y) \) notation with \( 0 \)-based indexing.

inputFormat

The input is given through standard input (stdin) in the following format:

n m
row1
row2
...
row_n
sx sy dx dy

Where:

  • n and m are integers representing the number of rows and columns of the grid.
  • Each of the next n lines contains a string of length m representing a row of the grid. Each character is either . (free cell) or # (blocked cell).
  • The last line contains four integers: sx, sy, dx, and dy which are the starting and destination coordinates respectively (0-indexed).

outputFormat

Output a single integer: the minimum number of moves required for the robot to reach the destination. If it is impossible to reach the destination, output -1. The answer should be printed to standard output (stdout).

## sample
5 5
.....
.#.#.
..#..
.###.
.....
0 0 4 4
8

</p>