#P3869. [TJOI2009] 宝藏
[TJOI2009] 宝藏
Description
To search for the legendary treasure, Xiao Ming enters a maze. We describe this maze with an -by- matrix, where each position represents a square cell:
- The character
.means the cell is passable. - The character
#means the cell is impassable. - There are mechanisms in the maze. The -th mechanism works as follows:
- Whenever Xiao Ming steps onto the cell at row , column , the cell at row , column toggles its state (if that cell is passable at that moment, it becomes impassable afterward; if it is impassable, it becomes passable afterward. The top-left cell is row , column ).
Given Xiao Ming’s current position, the treasure’s position, the current state of every cell in the maze, and all mechanism descriptions, determine the minimum number of steps Xiao Ming still needs to reach the treasure (he cannot move outside the maze boundary. At the start, both Xiao Ming’s cell and the treasure’s cell are passable. No mechanism appears at the start or the end, and no mechanism affects these two cells).
Input Format
The first line contains two integers: and .
Lines to each contain a string of length , describing the current state of the maze: . means a passable cell, # means an impassable cell, S marks the start, and T marks the treasure’s position.
Line contains an integer , the number of mechanisms. Then lines follow, each containing integers , describing one mechanism.
Output Format
Output a single integer: the minimum number of steps Xiao Ming needs to reach the treasure. The testdata guarantees the treasure is reachable.
5 5
S.#..
#####
..#..
##.#.
...#T
6
1 5 4 2
1 4 3 3
5 1 3 3
1 4 4 5
1 2 1 3
1 5 2 1
22
Hint
Constraints
For all testdata, , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号