#P7904. 火烧の云
火烧の云
Description
A country road can be approximated as an matrix. There are many kinds of road tiles; only a small part of them are listed here:
#: Pond, impassable.|: Straight-type road. In the vertical direction you can only go straight; in the horizontal direction you can only turn left or right.-: Straight-type road. In the horizontal direction you can only go straight; in the vertical direction you can only turn left or right./: Turning-type road. In the vertical direction you can only turn right; in the horizontal direction you can only turn left.\: Turning-type road. In the vertical direction you can only turn left; in the horizontal direction you can only turn right..: Four-way intersection. You can go straight, turn left, or turn right.S: Entrance. Entering this tile from outside the countryside takes no time.E: Exit. Leaving the countryside from this tile takes no time.
Forward-type road: After reaching this cell, the direction must spend time to turn to ?. If the incoming direction is already ?, then it costs no time and you must jump one cell.
<:? = West.>:? = East.^:? = North.v:? = South.
In short: you cannot move against the direction of this kind of road; if you move in a direction perpendicular to it, you spend time to turn to its direction; if you move along its direction, then it costs no time and you must move two cells at once.
Straight-type roads, turning-type roads, four-way intersections, and forward-type roads cost units of time, respectively.
Due to the strange structure of the countryside, there may be more than one entrance and exit.
Find the shortest time from any entrance to any exit. The directions when entering and leaving are not restricted.
Brief statement
Given an map, find the shortest time from S to E. Note that there may be multiple start points and multiple end points.
Input Format
The first line contains six positive integers separated by spaces.
Then follow an -row, -column character matrix, which contains the above kinds of characters: #、|、-、 /、\、.、<、>、^、v、S、E.
Output Format
Output the minimum time needed to reach the destination from a start point. If the destination cannot be reached, output -1.
3 3 6 5 4 3
S..
...
..E
12
3 3 1 2 7 8
S.E
\-/
...
5
3 3 1 2 7 8
SEE
\-/
SSS
0
1 4 6 5 4 3
S>#E
0
2 2 6 5 4 3
#E
S^
3
1 3 1 2 7 8
S#E
-1
Hint
Sample Explanation
Sample #1: Starting from and ending at , the path is $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)\rightarrow(3,3)$. It passes through four-way intersections, each with cost , so the total cost is .
Sample #2: Starting from and ending at , the path is $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(1,3)$. It passes through turning-type roads and straight-type road, so the total cost is .
Sample #3: Choose S at and E at . The entrance and exit are adjacent, so the cost is .
Sample #4: Using the forward-type road > you can jump one cell directly to E. Jumping one cell costs no time.
Sample #5: A forward-type road can also be used for turning; in this case its behavior is the same as /, and turning also requires time.
Sample #6: Here # cannot be passed, so there is no path from S to E.
Constraints
| Subtask ID | Score | Special Property | |
|---|---|---|---|
The number of characters S and E |
|||
Please pay attention to the impact of constant factors on program efficiency in this problem.
For of the testdata: , .
Translated by ChatGPT 5
京公网安备 11011102002149号