#P6742. [BalticOI 2014] Portals (Day2)
[BalticOI 2014] Portals (Day2)
Description
Given an maze, each cell contains one type of tile:
#Wall: you cannot walk onto it and cannot pass through it..Floor: you can walk on it.SStart: the player starts here; there is exactly one.CGoal: the player must reach here; there is exactly one.
Now the maze is expanded by adding a layer of # tiles around it, becoming a maze.
Also, at any time, the player can shoot a portal in one of the four directions: up, left, down, right. When a portal is shot, it keeps flying in the shooting direction until it touches a wall. At that moment, the portal is placed on that wall.
Moving one cell takes unit of time, and traveling through a portal also takes unit of time.
Find the minimum time needed to reach the goal from the start.
If the statement is hard to understand, please refer to the samples.
Input Format
The first line contains two integers , representing the maze size.
The next lines each contain characters, describing the maze.
Output Format
Output one integer: the minimum time to reach the goal.
4 4
.#.C
.#.#
....
S...
4
Hint
Sample Explanation
For Sample , after we simulate the maze and add # around it, it looks like the following figure:

The humanoid object is S, and the cake-shaped object is C.
As shown, at least units of time are needed.
Constraints
This problem uses bundled testdata.
- Subtask 1 (11 pts): .
- Subtask 2 (20 pts): .
- Subtask 3 (20 pts): , and every
.has at least one#adjacent to it. - Subtask 4 (19 pts): .
- Subtask 5 (30 pts): no special constraints.
For of the testdata, .
This problem forces optimization.
Notes
Translated from BalticOI 2014 Day2 B Portals。
Translated by ChatGPT 5
京公网安备 11011102002149号