例:
#include <bits/stdc++.h>
using namespace std;
const int N = 200 + 10;
const int inf = -1;
struct State {
int x, y, energy;
};
int h, w, m;
char grid[N][N];
int e_map[N][N];
vector<tuple<int, int, int>> drugs;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool bfs(int sx, int sy, int ex, int ey) {
queue <State> q;
q.push({sx, sy, 0});
e_map[sx][sy] = 0;
while (!q.empty()) {
auto[x, y, energy] = q.front();
q.pop();
if (x == ex && y == ey) {
return true;
}
for (auto&[rx, ry, e] : drugs) {
if (x == rx && y == ry && e > energy) {
energy = e;
}
}
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >=w || grid[nx][ny] == '#') {
continue;
}
if (energy > 0 && e_map[nx][ny] < energy - 1) {
e_map[nx][ny] = energy - 1;
q.push({nx, ny, energy - 1});
}
}
}
return false;
}
int main() {
cin >> h >> w;
int sx, sy, ex, ey;
for (int i = 0; i < h; ++i) {
for(int j = 0; j < w; ++j) {
cin >> grid[i][j];
if (grid[i][j] == 'S') {
sx = i, sy = j;
}
if (grid[i][j] == 'T'){
ex = i, ey = j;
}
e_map[i][j] = inf;
}
}
cin >> m;
drugs.resize(m);
for (int i = 0; i < m; ++i) {
int r, c, e;
cin >> r >> c >> e;
drugs[i] = {r - 1, c - 1, e};
}
if (bfs(sx, sy, ex, ey)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}