#K14001. Shortest Path in a Labyrinth

    ID: 24037 Type: Default 1000ms 256MiB

Shortest Path in a Labyrinth

Shortest Path in a Labyrinth

You are given a labyrinth represented as an \(N\times M\) grid. Each cell in the grid is either an empty cell (denoted by '.') or a blocked cell (denoted by '#'). You are also given the starting position \((s_x, s_y)\) and the treasure position \((t_x, t_y)\). Your task is to determine the minimum number of moves required to reach the treasure from the starting position, moving only up, down, left, or right through empty cells.

If it is not possible to reach the treasure, output \(-1\).

Note: Both positions are given in 0-indexed format.

inputFormat

The input is given from standard input (stdin) in the following format:

  1. The first line contains two integers (N) and (M) representing the number of rows and columns of the labyrinth.
  2. The next (N) lines each contain a string of length (M) consisting of characters '.' and '#' describing the labyrinth.
  3. The following line contains two integers (s_x) and (s_y) representing the starting cell coordinates (0-indexed).
  4. The last line contains two integers (t_x) and (t_y) representing the treasure cell coordinates (0-indexed).

outputFormat

Output a single integer to standard output (stdout): the minimum number of moves required to reach the treasure from the starting position. If the treasure cannot be reached, output -1.## sample

5 5
.....
.#.#.
.#.#.
.#...
...#.
0 0
4 4
8