#abc348d. D - Medicines on Grid

D - Medicines on Grid

Score: 425425 points

问题陈述

有一个 HHWW 列的网格。用 (i,j)(i, j) 表示从顶部数第 ii 行,从左边数第 jj 列的单元格。每个单元格的状态由字符 Ai,jA_{i,j} 表示,含义如下:

  • .: 一个空单元格。
  • #: 一个障碍物。
  • S: 一个空单元格,也是起点。
  • T: 一个空单元格,也是终点。

高桥可以从他当前的单元格移动到垂直或水平相邻的空单元格,消耗 11 能量。如果他的能量是 00,他就不能移动,也不能离开网格。

网格中有 NN 个药水。第 ii 个药水位于空单元格 (Ri,Ci)(R_i, C_i),可以用来设置能量为 EiE_i。注意,能量不一定会增加。他可以在当前单元格使用药水。使用过的药水会消失。

高桥从起点开始,能量为 00,想要到达终点。确定这是否可能。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is a grid with HH rows and WW columns. Let (i,j)(i, j) denote the cell at the ii-th row from the top and the jj-th column from the left. The state of each cell is represented by the character Ai,jA_{i,j}, which means the following:

  • .: An empty cell.
  • #: An obstacle.
  • S: An empty cell and the start point.
  • T: An empty cell and the goal point.

Takahashi can move from his current cell to a vertically or horizontally adjacent empty cell by consuming 11 energy. He cannot move if his energy is 00, nor can he exit the grid.

There are NN medicines in the grid. The ii-th medicine is at the empty cell (Ri,Ci)(R_i, C_i) and can be used to set the energy to EiE_i. Note that the energy does not necessarily increase. He can use the medicine in his current cell. The used medicine will disappear.

Takahashi starts at the start point with 00 energy and wants to reach the goal point. Determine if this is possible.

Constraints

  • 1H,W2001 \leq H, W \leq 200
  • Ai,jA_{i, j} is one of ., #, S, and T.
  • Each of S and T exists exactly once in Ai,jA_{i, j}.
  • 1N3001 \leq N \leq 300
  • 1RiH1 \leq R_i \leq H
  • 1CiW1 \leq C_i \leq W
  • (Ri,Ci)(Rj,Cj)(R_i, C_i) \neq (R_j, C_j) if iji \neq j.
  • ARi,CiA_{R_i, C_i} is not #.
  • 1EiHW1 \leq E_i \leq HW

Input

The input is given from Standard Input in the following format:

HH WW

A1,1A_{1, 1}A1,2A_{1, 2}\cdotsA1,WA_{1, W}

A2,1A_{2, 1}A2,2A_{2, 2}\cdotsA2,WA_{2, W}

\vdots

AH,1A_{H, 1}AH,2A_{H, 2}\cdotsAH,WA_{H, W}

NN

R1R_1 C1C_1 E1E_1

R2R_2 C2C_2 E2E_2

\vdots

RNR_N CNC_N ENE_N

Output

If Takahashi can reach the goal point from the start point, print Yes; otherwise, print No.

Sample Input 1

4 4
S...
#..#
#...
..#T
4
1 1 3
1 3 5
3 2 1
2 3 1

Sample Output 1

Yes

For example, he can reach the goal point as follows:

  • Use medicine 11. Energy becomes 33.
  • Move to (1,2)(1, 2). Energy becomes 22.
  • Move to (1,3)(1, 3). Energy becomes 11.
  • Use medicine 22. Energy becomes 55.
  • Move to (2,3)(2, 3). Energy becomes 44.
  • Move to (3,3)(3, 3). Energy becomes 33.
  • Move to (3,4)(3, 4). Energy becomes 22.
  • Move to (4,4)(4, 4). Energy becomes 11.

There is also medicine at (2,3)(2, 3) along the way, but using it will prevent him from reaching the goal.

Sample Input 2

2 2
S.
T.
1
1 2 4

Sample Output 2

No

Takahashi cannot move from the start point.

Sample Input 3

4 5
..#..
.S##.
.##T.
.....
3
3 1 5
1 2 3
2 2 1

Sample Output 3

Yes