#K68937. Minimum Moves in Grid

    ID: 32975 Type: Default 1000ms 256MiB

Minimum Moves in Grid

Minimum Moves in Grid

You are given a grid of size \(n \times m\) where each cell is either empty (represented by '0') or an obstacle (represented by '1'). You are also given a starting cell and a target cell using 1-indexed coordinates. Your task is to determine the minimum number of moves required to reach the target cell from the starting cell. A move consists of moving one cell up, down, left, or right, and you cannot move into a cell with an obstacle. If it is impossible to reach the target cell, print \(-1\).

Note: The grid is represented by strings with no spaces. The starting and target positions are given in 1-indexed format, so you must convert them to 0-indexed format before processing.

inputFormat

The first line contains an integer \(T\) representing the number of test cases. Each test case is described as follows:

  • The first line contains two integers \(n\) and \(m\), the number of rows and columns of the grid.
  • The next \(n\) lines each contain a string of length \(m\) consisting of characters '0' and '1'. Here, '0' denotes an empty cell and '1' denotes an obstacle.
  • The next line contains four integers \(sx\), \(sy\), \(tx\), \(ty\) which represent the starting cell and the target cell (in 1-indexed format).

All input is to be read from standard input (stdin).

outputFormat

For each test case, print a single integer on a separate line representing the minimum number of moves required to reach the target cell from the start cell. If the target is unreachable, print \(-1\). The output should be written to standard output (stdout).

## sample
4
3 3
000
010
000
1 1 3 3
3 3
000
000
000
1 1 3 1
3 3
010
010
010
1 1 3 3
4 4
0000
0010
0000
0100
1 1 4 4
4

2 -1 6

</p>