#K88657. Taco Treasure Hunt

    ID: 37357 Type: Default 1000ms 256MiB

Taco Treasure Hunt

Taco Treasure Hunt

You are given a grid representing a forest where each cell is either open (denoted by '.') or blocked by an obstacle (denoted by '#'). Starting from the top-left corner (0, 0), your task is to find the minimum number of moves required to reach a treasure located at cell \((tx, ty)\). You may move in the four cardinal directions (up, down, left, right), and you cannot step onto a cell with an obstacle.

If the starting cell or the treasure cell is blocked, or if the treasure cannot be reached, output -1. Otherwise, output the minimum number of moves. Input consists of multiple test cases and terminates with a test case where n = 0 and m = 0.

The movement from one cell to an adjacent cell counts as one move. Mathematically, if you denote a move from cell \((x, y)\) to \((x+dx, y+dy)\) with \(dx, dy \in \{-1, 0, 1\}\) (with the restriction that \(|dx|+|dy|=1\)), then the path length is the sum of moves along the chosen route.

inputFormat

The input is given via standard input and contains multiple test cases. Each test case is formatted as follows:

  • The first line contains two integers \(n\) and \(m\) indicating the number of rows and columns in the forest grid.
  • The following \(n\) lines each contain a string of length \(m\) composed of characters '.' and '#' representing the forest.
  • The next line contains two integers \(tx\) and \(ty\) representing the treasure's row and column indices.

A line with 0 0 signifies the end of input.

outputFormat

For each test case, output a single integer on a new line that indicates the minimum number of moves required to reach the treasure from the starting point at \((0, 0)\). If it is impossible to reach the treasure, output -1.

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