#K68157. Taco Soldier Rescue

    ID: 32802 Type: Default 1000ms 256MiB

Taco Soldier Rescue

Taco Soldier Rescue

A brave soldier must rescue a captive hidden within a grid-like battlefield. The grid is an \(N \times M\) matrix where each cell is either free (denoted by .) or blocked by an obstacle (denoted by #). The soldier starts at position \((S_1, S_2)\) and needs to move to the captive at position \((C_1, C_2)\).

The soldier can move one step at a time, either up, down, left, or right. Your task is to determine the minimum number of moves required to reach the captive. If the captive cannot be reached, output \(-1\).

All positions are 0-indexed. If the starting position is the same as the captive's, the answer is \(0\).

inputFormat

The input is provided via stdin in the following format:

  • 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\) describing a row of the grid. A dot . represents an open cell, and a hash # represents an obstacle.
  • The following line contains two integers \(S_1\) and \(S_2\): the starting row and column of the soldier.
  • The next line contains two integers \(C_1\) and \(C_2\): the row and column of the captive.

outputFormat

Output a single integer to stdout, which is the minimum number of moves required for the soldier to reach the captive. If it is impossible, output -1.

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