#P3818. 小A和uim之大逃离 II
小A和uim之大逃离 II
Description
In a flash, a huge matrix of rows and columns appears on the ground. Each cell is either empty land . or an obstacle #.
They start at and must escape to the exit at . They can move one cell up, down, left, or right; each such move counts as one operation. They still have the magic potion from their last adventure. If they drink it in one go, they can instantly teleport by the relative vector ; that is, if their current position is , the new position becomes . This also counts as one operation, but they can use this operation at most once (they may also choose not to drink the potion).
This is a dangerous place. They want to know the minimum number of operations needed to get out. However, it might be impossible to escape; in that case, they can only await their fate.
Input Format
The first line contains four integers , , , , whose meanings are explained in the description.
The next lines each contain a string of length , consisting only of . and #.
Output Format
Output a single integer representing the minimum number of operations needed to escape. If they cannot escape, output .
3 6 2 1
...#..
..##..
..#...
5
3 7 2 1
..#..#.
.##.##.
.#..#..
-1
6 6 -2 0
.#....
.#.#..
.####.
.#..#.
.##.#.
....#.
21
Hint
Sample explanation 1.
drink the magic potion .
Sample explanation 2.
Since there is only one bottle of magic potion, they cannot escape.
Sample explanation 3.
and can also be or negative.
Constraints and Notes.
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号