#K71752. Drone Delivery Optimization

    ID: 33601 Type: Default 1000ms 256MiB

Drone Delivery Optimization

Drone Delivery Optimization

You are given a grid of size \(n \times m\) representing a city map. Each cell in the grid is either an empty cell denoted by a period ('.') or a building denoted by a hash ('#'). A drone needs to deliver a package from a starting coordinate \((sx, sy)\) to a destination coordinate \((dx, dy)\) by moving one step at a time in one of the four cardinal directions (up, down, left, right).

Your task is to determine the minimum number of moves required to complete the delivery. If it is not possible to reach the destination due to buildings blocking the way, output \(-1\).

The movement can be mathematically described as: Given a cell \((x, y)\), the possible moves are to \((x-1, y)\), \((x+1, y)\), \((x, y-1)\), and \((x, y+1)\), as long as the new cell is within the grid bounds and is not a building.

inputFormat

The input is given via standard input with the following structure:

  1. An integer \(t\) representing the number of test cases.
  2. For each test case:
    1. A line containing two integers \(n\) and \(m\), the number of rows and columns in the grid.
    2. Next \(n\) lines, each containing a string of length \(m\), which represents the grid. A dot ('.') represents an empty cell and a hash ('#') represents a building.
    3. A line with four integers: \(sx\) \(sy\) \(dx\) \(dy\) which are the starting coordinates and destination coordinates respectively (0-indexed).

outputFormat

For each test case, output the minimum number of moves required to deliver the package. If it is impossible to reach the destination, output \(-1\). Each result should be printed on its own line.

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

-1 0 4

</p>