#K65062. Minimum Moves in a Grid

    ID: 32114 Type: Default 1000ms 256MiB

Minimum Moves in a Grid

Minimum Moves in a Grid

You are given an n x n grid representing a city, where each cell is either passable or blocked. A cell represented by . is open, while a cell denoted by # is blocked. You are also given a starting cell and a destination cell.

Your task is to determine the minimum number of moves required to go from the start cell to the destination cell, moving only up, down, left, or right. If it is impossible to reach the destination, print -1.

The moves can be mathematically understood by the formula:

\(moves = \min\{d \mid d \text{ is the shortest Manhattan-like distance considering obstacles}\}\)

Note that both the starting and the destination cell must be passable, i.e. they must not contain a #.

inputFormat

The first line contains an integer n (the size of the grid).
Then follow n lines, each containing a string of length n, representing the grid. Each character is either . (open) or # (blocked).

The next line contains two integers x1 and y1 separated by a space, representing the starting cell (0-indexed).
The final line contains two integers x2 and y2 separated by a space, representing the destination cell (0-indexed).

outputFormat

Output a single integer: the minimum number of moves required to get from the starting cell to the destination cell. If it is impossible, output -1.

## sample
4
....
.#..
..#.
....
0 0
3 3
6

</p>