Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #define N 1005
- #define y1 Y1
- using namespace std;
- int dist1[N][N], dist2[N][N], qx[N * N], qy[N * N], l, r, x, y, x1, y1, x2, y2;
- char a[N][N];
- bool start = false;
- void add(int x, int y) {
- qx[r] = x;
- qy[r++] = y;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int n, m;
- scanf("%d %d ", &n, &m);
- memset(dist1, 127, sizeof(dist1));
- memset(dist2, 127, sizeof(dist2));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++) {
- cin >> a[i][j];
- if (a[i][j] == 'T')
- x2 = i, y2 = j, a[i][j] = '.';
- if (a[i][j] == '.' && !start) {
- start = true;
- x = i, y = j;
- }
- if (a[i][j] == '.')
- x1 = i, y1 = j;
- }
- add(x, y);
- dist1[x][y] = 0;
- while (l < r) {
- int nowx = qx[l], nowy = qy[l++];
- for (int dx = -1; dx < 2; dx++)
- for (int dy = -1; dy < 2; dy++)
- if (abs(dx) + abs(dy) == 1) {
- if (a[nowx + dx][nowy + dy] == '.') {
- if (dist1[nowx + dx][nowy + dy] > dist1[nowx][nowy] + 1) {
- dist1[nowx + dx][nowy + dy] = dist1[nowx][nowy] + 1;
- add(nowx + dx, nowy + dy);
- }
- }
- }
- }
- l = r = 0;
- add(x2, y2);
- dist2[x2][y2] = 0;
- while (l < r) {
- int nowx = qx[l], nowy = qy[l++];
- for (int dx = -1; dx < 2; dx++)
- for (int dy = -1; dy < 2; dy++)
- if (abs(dx) + abs(dy) == 1) {
- if (a[nowx + dx][nowy + dy] == '.') {
- if (dist2[nowx + dx][nowy + dy] > dist2[nowx][nowy] + 1) {
- dist2[nowx + dx][nowy + dy] = dist2[nowx][nowy] + 1;
- add(nowx + dx, nowy + dy);
- }
- }
- }
- }
- if (dist1[x1][y1] < dist2[x1][y1])
- puts("Yes");
- else
- puts("No");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement