#P2254. [NOI2005] 瑰丽华尔兹
[NOI2005] 瑰丽华尔兹
Description
Assume the ballroom is an -by- grid. Some cells have furniture piled on them, and the rest are empty. The piano can slide on empty cells, but it cannot hit furniture or slide out of the ballroom; otherwise, both the piano and the furniture would be damaged, and the captain would be upset. At each moment, the piano slides one cell to an adjacent cell in the direction of the ship’s tilt. The adjacent cell can be east, west, south, or north. Emily can choose to cast magic or not: if she does not cast magic, the piano slides; if she does, the piano stays where it is.
Emily is an angel, and she knows the ship’s tilt over each time interval. She wants to make the piano travel as long a distance as possible in the ballroom, which will make 1900 very happy and also help Tooney’s seasickness. But Emily is still too young to calculate it, so she hopes you can help her.
Input Format
The first line contains 5 numbers , , , and . and describe the size of the ballroom, and and are the initial position of the piano. We describe the ship’s tilt by time intervals, and time starts from . For example: “tilting east during , and tilting north during ”. Therefore, is the number of intervals.
The next lines, each with characters, describe the furniture in the ballroom. In row , column , if the character is . then that cell is empty; if it is x then there is furniture.
The next lines describe the time intervals in order, in the format: (). It means that during the time interval , the ship tilts toward direction . is one of , , , , which denote north, south, west, and east respectively (corresponding to up, down, left, and right in the grid). The input guarantees that the intervals are continuous, that is, , (), .
Output Format
Output a single line containing one integer, the maximum sliding distance of the piano (i.e., the number of cells).
4 5 4 1 3
..xx.
.....
...x.
.....
1 3 4
4 5 1
6 7 3
6
Hint
The piano’s sliding path:

When the piano is at the "×" position, the angel uses magic once, so the total sliding length is .
Constraints:
- For of the testdata, , .
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号