#P2254. [NOI2005] 瑰丽华尔兹

    ID: 1219 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划数据结构单调队列队列dp2005NOI 系列

[NOI2005] 瑰丽华尔兹

Description

Assume the ballroom is an NN-by-MM 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 NN, MM, xx, yy and KK. NN and MM describe the size of the ballroom, and xx and yy are the initial position of the piano. We describe the ship’s tilt by time intervals, and time starts from 11. For example: “tilting east during [1,3][1, 3], and tilting north during [4,5][4, 5]”. Therefore, KK is the number of intervals.

The next NN lines, each with MM characters, describe the furniture in the ballroom. In row ii, column jj, if the character is . then that cell is empty; if it is x then there is furniture.

The next KK lines describe the KK time intervals in order, in the format: si ti dis_i\ t_i\ d_i (1iK1 \leq i \leq K). It means that during the time interval [si,ti][s_i, t_i], the ship tilts toward direction did_i. did_i is one of 11, 22, 33, 44, 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, s1=1s_1 = 1, si=ti1+1s_i = t_{i-1} + 1 (1<iK1 < i \leq K), tK=Tt_K = T.

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 66.

Constraints:

  • For 50%50\% of the testdata, 1N,M2001 \leq N, M \leq 200, T200T \leq 200.
  • For 100%100\% of the testdata, 1N,M2001 \leq N, M \leq 200, K200K \leq 200, T40000T \leq 40000.

Translated by ChatGPT 5