#P9065. [yLOI2023] 云梦谣
[yLOI2023] 云梦谣
Background
Come back and have a Yunmeng dream, a great dream is good.
Plant flowers and smell the wine, wake up, get drunk, laugh and smile.
The world is vast, the road back is long and the mountains are high; the rarest thing is to steal half a day of leisure.
Sometimes be confused, do not ask about worldly affairs, and do not know.
— Yin Lin & Mu Han, Yunmeng Ballad
Problem Description
“Hey, Gouqi, you silly dog, you’re stealing food again! Watch me teach you a lesson!”
Duo Yi ran out of the yard angrily, holding a duster, but Gouqi had already disappeared.
Yunmeng Courtyard can be seen as a grid of rows and columns. The cell in row and column is denoted as . Each cell either has a height (where is a positive integer), or it is an obstacle and cannot be passed through. (For convenience, we define that an obstacle has equal to .) In addition, there are designated cells in Yunmeng Courtyard where sword flight is allowed. Initially, Duo Yi and Gouqi are located at cells and respectively.
Duo Yi is not very skilled at sword flight yet, and she cannot control the flying height well. Therefore, at any moment, when Duo Yi is on cell , she can perform one of the following actions:
- Move to one of the adjacent cells , , , (she cannot move outside the grid, and cannot move onto an obstacle).
- If sword flight is allowed on cell , then Duo Yi can fly to another cell that also allows sword flight and has the same height as cell .
- Use immortal magic to change the height of the current cell to any positive integer.
Each action above costs unit of time.
“Hmph, you silly dog, keep running!” After saying that, Duo Yi chased after him. Duo Yi still needs to continue today’s cultivation as soon as possible, so she wants to know the minimum time needed to reach cell .
Input Format
The first line contains three integers, which are the number of rows , the number of columns , and the number of cells where sword flight is allowed .
Then follow lines, each containing integers. The -th number in the -th line represents the height of cell . The data guarantees that and are not .
Then follow lines, each containing two integers and , representing the coordinates of a cell where sword flight is allowed. The data guarantees that these cells have pairwise distinct coordinates.
Output Format
Output one integer in a single line, indicating the minimum time needed for Duo Yi to reach . If Duo Yi cannot reach it, output -1.
4 4 2
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 1
3 4
3
4 4 3
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 1
2 4
4 1
4
2 5 0
1 0 3 3 4
2 3 4 0 5
7
4 4 3
1 1 1 0
1 1 0 1
1 0 1 1
0 1 1 1
1 1
2 1
3 3
3
Hint
Sample 1 Explanation
In the -st unit of time, Duo Yi changes the height of the current cell to .
In the -nd unit of time, Duo Yi flies from cell to by sword flight.
In the -rd unit of time, Duo Yi moves from cell to and catches up with Gouqi.
Sample 2 Explanation
In the -st unit of time, Duo Yi flies from cell to by sword flight.
In the -nd unit of time, Duo Yi moves from cell to .
In the -rd unit of time, Duo Yi moves from cell to .
In the -th unit of time, Duo Yi moves from cell to and catches up with Gouqi.
Constraints

For all test points, it is guaranteed that , .
Notes
Please pay attention to the impact of large input on program efficiency. Choose an appropriate input method to avoid TLE.
Additional Information
This problem has 5 additional sample files. See dream.zip in the attachments.
Epilogue
However, even though Duo Yi looks angry now, after she catches up with Gouqi, she probably will not really have the heart to hit him. “Hehe, after today’s cultivation ends, what should I eat?” In Yunmeng Courtyard, with waterfalls hanging down and green bamboo all around, cultivating and training the body without envying worldly noise should be the most carefree thing in the world.
Translated by ChatGPT 5