#K44997. Minimum Moves in a Grid

    ID: 27655 Type: Default 1000ms 256MiB

Minimum Moves in a Grid

Minimum Moves in a Grid

You are given a grid consisting of n rows and m columns. Each cell in the grid is either a free space (represented by '.') or an obstacle (represented by '#'). You are also provided with two pairs of integers representing the start position and the target position on the grid (0-indexed).

Your task is to compute the minimum number of moves required to move from the start position to the target position. In one move, you can travel to an adjacent cell (up, down, left, or right) if it is a free space. If the target cannot be reached, output -1.

The movement cost is defined as 1 move per step. Use the Breadth-First Search (BFS) algorithm to determine the minimum number of moves required.

inputFormat

The input is read from stdin and consists of the following:

  1. The first line contains two space-separated integers: n and m, the number of rows and columns of the grid respectively.
  2. The next n lines each contain a string of length m representing a row of the grid. Each character is either . (free space) or # (obstacle).
  3. The last line contains four space-separated integers: sx, sy, tx, ty, representing the starting position and the target position respectively (with 0-indexed row and column indices).

outputFormat

The output is a single integer printed to stdout which represents the minimum number of moves required to reach the target from the start. If the target is unreachable, output -1.

## sample
3 4
....
..#.
....
0 0 2 3
5