|
| 1 | +```cpp |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <queue> |
| 5 | +#include <tuple> |
| 6 | +#include <algorithm> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +int dx[4] = { 1,0,-1,0 }; |
| 10 | +int dy[4] = { 0,1,0,-1 }; |
| 11 | +int N, M, sx, sy; |
| 12 | +char A[700][700]{}; |
| 13 | +int L[700][700]{}, X[700][700]{}; |
| 14 | + |
| 15 | +int main() |
| 16 | +{ |
| 17 | + cin.tie(0)->sync_with_stdio(0); |
| 18 | + |
| 19 | + cin >> N >> M; |
| 20 | + queue<tuple<int, int, int>> Q; |
| 21 | + bool vis[700][700]{}; |
| 22 | + for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { |
| 23 | + cin >> A[i][j]; |
| 24 | + if (A[i][j] == 'V') Q.emplace(i, j, 0), vis[i][j] = 1; |
| 25 | + if (A[i][j] == 'Y') sx = i, sy = j; |
| 26 | + } |
| 27 | + |
| 28 | + for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) X[i][j] = 2e9; |
| 29 | + while (!Q.empty()) { |
| 30 | + auto[x, y, t] = Q.front(); |
| 31 | + Q.pop(); |
| 32 | + X[x][y] = min(X[x][y], t); |
| 33 | + for (int k = 0; k < 4; k++) { |
| 34 | + int xx = x + dx[k], yy = y + dy[k]; |
| 35 | + while (0 <= xx && xx < N && 0 <= yy && yy < M && A[xx][yy] != 'I') { |
| 36 | + X[xx][yy] = min(X[xx][yy], t); |
| 37 | + xx += dx[k], yy += dy[k]; |
| 38 | + } |
| 39 | + xx = x + dx[k], yy = y + dy[k]; |
| 40 | + if (xx < 0 || xx >= N || yy < 0 || yy >= M || A[xx][yy] == 'I' || vis[xx][yy]) continue; |
| 41 | + vis[xx][yy] = 1; |
| 42 | + Q.emplace(xx, yy, t + 1); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) vis[i][j] = 0; |
| 47 | + vis[sx][sy] = 1; |
| 48 | + Q.emplace(sx, sy, 0); |
| 49 | + while (!Q.empty()) { |
| 50 | + auto[x, y, t] = Q.front(); |
| 51 | + Q.pop(); |
| 52 | + if (A[x][y] == 'T') return cout << "YES", 0; |
| 53 | + for (int k = 0; k < 4; k++) { |
| 54 | + int xx = x + dx[k], yy = y + dy[k]; |
| 55 | + if (xx < 0 || xx >= N || yy < 0 || yy >= M || A[xx][yy] == 'I' || vis[xx][yy] || t + 1 >= X[xx][yy]) continue; |
| 56 | + Q.emplace(xx, yy, t + 1); |
| 57 | + vis[xx][yy] = 1; |
| 58 | + } |
| 59 | + } |
| 60 | + cout << "NO"; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +``` |
0 commit comments